02-02-2015 09:54 AM
hi,
I try to delete a file after constructing the file name based on info passed into the function. it fails. in debug mode, I can see that the file name is correct. I narrow the problem down to this:
if I use a local char this way:
char *ThisFile = "C:\Inetpub\ftproot\Saved Settings\zz.txt";
int status = remove (ThisFile);
The content of ThisFile is corrupt as shown in the picture - file 1. But if I use it this way:
char *ThisFile = "C:\\Inetpub\\ftproot\\Saved Settings\\zz.txt";
int status = remove (ThisFile);
It succeeds, but the content of ThisFile changes, as shown in the picture - file 2.
Problem: I pass the Folder Name into the function as a string, whose value is seen as:
C:\Inetpub\ftproot\Saved Settings\
Then I construct the file name from it. It fails all the time. The removal of the '\' character is to blame, as the local method works with the '\\' in place. What can I do in this case? I do not encounter this problem with remove() function. Other functions like fgetc() and fopen() do not give me this problem. Please help.
02-02-2015 10:15 AM
Hello hn_sofec!
The workaround you proposed is perfectly normal in C. In C, a character preceded by a '\' is escaped, meaning that the compiler treats it as having a special meaning.
Examples like this are:
Having said this, if you want to write "<tab character>some text<tab character>more text<newline>" in C, you have to write something like:
char *string = "\tsome text\tmore text\n";
A legitimate question for this problem, would be "how should I write a backslash in my string?" To do this, you would have to escape the slash ('\') character itself like this: \\.
Consequently writing a string that represents a path, in C, means writing double-slashes.
This means that:
C:\Program Files\Windows
is translated in C as:
char *path = "C:\\Program Files\\Windows";
Here is a related CVI help topic:
http://digital.ni.com/public.nsf/allkb/6C6A1CF8989553A086256AD30056DDDF
Best regards!
- Johannes
02-02-2015 10:38 AM
OK,
so I go through the pain of addding an extra'\' after each '\' ---> the reconstructed string has '\\' now. the remove() function still does not work. Access denied again. Any help???
02-02-2015 11:30 AM - edited 02-02-2015 11:31 AM
I retry again and again, this is work I find out: it only works with a fixed declaration:
char *ThisFile = "C:\\Inetpub\\ftproot\\Saved Settings\\zz.txt";
int status = remove (ThisFile);
Any other method does not work, even if the string passed into remove() has \\ or \ in the place of the backward slash. By that I mean contructing the file name string from other strings. Any help?
02-02-2015 11:55 AM - edited 02-02-2015 11:56 AM
s
02-03-2015 01:31 AM
Can you post the code you are actually using to build up the string?
Alternatively, you could try building up the string using MakePathname () command.
02-03-2015 01:31 AM - edited 02-03-2015 01:32 AM
by "contructing the file name string from other string" method, I assume you are referring to the fact that the method that does not work ISN'T passing the string name directly to te remove() function, like this:
remove ("C:\\Inetpub\\ftproot\\Saved Settings\\zz.txt");
By that, you actually mean that the other method which isn't working is performing a series of concatenations/copies and passing the end result to the remove() function.
Please provide more details about how you are creating that path string (e.g. pasting the actual code).
Best regards!
- Johannes
02-03-2015 12:01 PM
I fixed the issue. it is caused by improper fopen() / close(stream) from another function. the input string does not need to have '\' replaced with '\\'. thanks so much Johannes.