LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

remove function - Access denied

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.

Download All
0 Kudos
Message 1 of 8
(5,672 Views)

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:

  1. Tabs: \t
  2. Newlines: \n
  3. Null character: \0 (escaped zero)

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

0 Kudos
Message 2 of 8
(5,661 Views)

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???

0 Kudos
Message 3 of 8
(5,655 Views)

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?

0 Kudos
Message 4 of 8
(5,643 Views)

s

0 Kudos
Message 5 of 8
(5,640 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 8
(5,620 Views)

hn_sofec,

 

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

0 Kudos
Message 7 of 8
(5,619 Views)

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.

0 Kudos
Message 8 of 8
(5,592 Views)