LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace Double Backslash with Single Backslash

Solved!
Go to solution

I am trying to read a path from my .ini file. The problem is that when I wrote the path to the .ini file it wrote it with the double backslashes like so: C:\\My Documents\\a.ini. When I read it in and resave it I get C:\\\\My Documents\\\\a.ini. I wanted to know if anybody had a way to change C:\\My Documents\\a.ini back to c:\My Documents\a.ini. The \\ are neccessary in that I had to use strcat for certain parts of my code. So somehow I need to scan for the "\\"  and replace them with "\". I tried the following but I get an error when using "\".

 

char *p;

 

while(p = strchr(path, '\\'))

 *p='\';

 

Help me please!

0 Kudos
Message 1 of 5
(7,775 Views)
Solution
Accepted by topic author InquiringMind

It sounds like the saving code is unnecessarily doubling every backslash it encounters. One option would be to change that.

 

Also, note that in C, when you want to specify string or character constants in your code, certain characters must be escaped with a preceeding backslash. For example:

 

newline = '\n' or "\n"

tab = '\t' or "\t"

apostrophe in character constant = ' \' '

quotes in string constant = "he said \"hello\""

backslash = '\\' or "\\"

 

You see that since the backslash takes on this special meaning when in a string or character constant, it must also be escaped when you really just want a backslash character. That is why, in your code example, *p='\' is an error. Also, note that in your example, strchr(path, '\\') just finds the location of any backslash character, not necessarily two back to back. Also realize that if you are trying to remove characters from a string, just changing the character at a given position is not going to work. I would recommend something like this:

 

char *fixedPath = malloc(strlen(originalPath) + 1); // null checking omitted for brevity

for (char *src=originalPath, *dest = fixedPath; 1; ++src, ++dest)

{

   *dest = *src;

   if (*src== '\0') // end of source string. break the loop.

      break;

   if (*src==  '\\' && *(src+1) == '\\')

      ++src; // skips the extra backslash

}

 

Mert A.

National Instruments

Message 2 of 5
(7,745 Views)
when using the CVI provided instrument for reading/writing ini file, i never ever had to convert "\\" to "\". it is supposed to be done automatically by the code of the instrument. what method are you using for reading and saving your ini file ?
0 Kudos
Message 3 of 5
(7,741 Views)
Also, are you aware of MakePathname and SplitPath commands to treat pathnames?


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 4 of 5
(7,722 Views)

Assuming you are using the ini-file instrument driver, use Ini_PutRawString instead of Ini_PutString while saving values to the ini-file.

 

Here is an excerpt from the function help:

 

" ... you must use Ini_PutRawString if you do not want the backslashes in the pathname to be converted into double backslashes in the destination .ini file ... The non-raw versions of the Ini_GetString functions automatically recognize and remove the escape codes inserted by Ini_PutString ..."

 

Hope this helps, 

S. Eren BALCI
IMESTEK
Message 5 of 5
(7,691 Views)