0

I have made this very simpel program and all it needs to do is deleting a file

#include <string>
#include <iostream>
#include <fstream>
int main()
{

string x = "C:\Users\dino\Documents\profiles\fil.txt";
if(remove(x.c_str()) != 0)
    {
        perror("Error deleting ");
    }
return 0;

}

But the perror gives me Invalid argument

Can't i choose the directory of the file I want to delete? if not, is there another way i can?

Sumsar1812
  • 616
  • 1
  • 9
  • 32

1 Answers1

2

You need to escape your string. . .

string x = "C:\\Users\\dino\\Documents\\profiles\\fil.txt";
remudada
  • 3,751
  • 2
  • 33
  • 60
  • oh yea my bad, but lets say i have a function that returns the directory to documents how can i then escape my string? – Sumsar1812 May 11 '14 at 10:58
  • depends on what that function returns? If you are getting a list of files, with paths, using a system call, these will probably already be escaped correctly. You would need to elaborate. . . . – remudada May 11 '14 at 11:03
  • I use `SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);` – Sumsar1812 May 11 '14 at 11:06
  • Then you would do this PathAppend(szPath, TEXT("New Doc.txt")); where szPath is the path you got from SHGetFolderPath – remudada May 11 '14 at 11:11
  • that gives me an error `error: C2664: 'PathAppendA' : cannot convert parameter 1 from 'std::string' to 'LPSTR'` – Sumsar1812 May 11 '14 at 11:24
  • http://stackoverflow.com/questions/3906515/cannot-convert-from-stdstring-to-lpstr – remudada May 11 '14 at 11:25