04-02-2009 12:38 PM
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!
Solved! Go to Solution.
04-03-2009 10:19 AM
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
04-03-2009 11:10 AM
04-03-2009 04:23 PM
04-06-2009 08:30 AM
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,