C++ How do I delete a file in a folder

August 5, 2010 at 13:56:32
Specs: Windows Vista
I found a c++ code for a program that deletes files on dreamincode.com. But it won't delete a file in a folder, I'm not sure how to make it delete stuff in a folder other than in C:\\
Here's the code.

// Snippet to delete files

#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;

int main ()
{

if( remove( "C:\\Name.txt" ) == -1 ) // You can change this to a directory
// of your file.
cout <<"Error deleting file\n";

else

cout <<"File successfully deleted\n";

return 0;

}

Could someone please edit the code to make it so that I can delete a file in a folder. Thanks :)

There is no spoon, I couldn't be bothered to upload one.


See More: C++ How do I delete a file in a folder

Report •


#1
August 6, 2010 at 08:49:01
Not obvious from what you've shown. What happens when you try?

Report •

#2
August 8, 2010 at 11:32:15
It fails and gives the error message/s of:

1>.\example.cpp(12) : warning C4129: 'J' : unrecognized character escape sequence
1>.\example.cpp(12) : warning C4129: 'D' : unrecognized character escape sequence
1>.\example.cpp(12) : warning C4129: 'S' : unrecognized character escape sequence

So it doesn't seem to be able to do folders. The unrecognised letters are the names of folders that the thing is in, such as in this case, J, my account on my laptop's name. D, for desktop, and S for the name of the file.

So with that information, could you please correct the program so it works with folders?

There is no spoon, I couldn't be bothered to upload one.


Report •

#3
August 8, 2010 at 12:45:00
Okay, now pretend I'm not standing over your shoulder, looking at your code.

What does line 12 of example.cpp look like?


Report •

Related Solutions

#4
August 8, 2010 at 14:37:22
the compiler requires escaped "\" for each "\" in your path...

char *mypath = "c:\path\path\file"; // error unrecognized escape
char *mypath = "c:\\path\\path\\file";

spaces in the path may throw things off too.

char *mypath = "c:\\\"documents and settings\"\\\"my documents\"\\myfile";

the quote in the path must be escaped or you'll get an unterminated string error.

HTH


Report •

Ask Question