Solved

Find + escape(replace) a (\)character in a path + filename from command line.

Posted on 2003-12-04
3
814 Views
Last Modified: 2010-08-05
I have written a small program that will be run on Win NT4 and W2k from a batch file.
I am using _findfirst to get the file attributes and I then open the file using fopen(name, a+) to read the number of records and I then append this info as a new record at the end of the file. (all works ok)

I want to be able to run the program from within the batch script using something like
program.exe c:\dir1\dir2\dirtest\filename.txt

I will copy the path+filename into a variable (say char *test) from argv[1].
I then need to escape(replace) the '\' characters in the string with '\\' so _findfirst and fopen will work correctly.
ie. I need test to contain c:\\dir1\\dir2\\dirtest\\filename.txt

I hope ive explained this clear enough as its been a while since ive been writing c.

int main(int argc, char* argv[])
{
  char *test;
  *test = argv[1]; //test now contains c:\dir1\dir2\dirtest\filename.txt
  //need to parse test so it contains c:\\dir1\\dir2\\dirtest\\filename.txt
...
...
}

Thanks.
0
Comment
Question by:diarmaid
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9874942
>>I then need to escape(replace) the '\' characters in the string with '\\' so _findfirst and fopen will work correctly.

No, you don't :o)

If you write

char* test="c:\\dir1\\dir2\\dirtest\\filename.txt";

the internal (binary) representation is 'c:\dir1\dir2\dirtest\filename.txt' - the double backslash is *ONLY* needed in C source code as a single backslash means something different. Input from the command line like the above will be processed correctly.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9875040
Hi diarmaid,

jkr's correct.

The backslash tells the compiler, "do not place this character (the backslash) in the string, insert the next character without interpretting it".  The entire backslash thing came to be so that you could put quotes into your message.  "This message contains a double quote (\")".  Without the backslash the quote in the parentheses would be interpretted as the end of string and you'd get a compilation error.

The C compiler interprets the backslash so that whatever follows it is placed into the string without question.  When your string contains double backslashes, the compiler treats the first one as an escape character and insterts the second into the actual string -- which is the effect that you want.

Kent
0
 
LVL 2

Author Comment

by:diarmaid
ID: 9875063
Thanks, as i said its been a while, forgot about that.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Absolute madness with managing time in C 4 80
Details to do the search 56 144
Beginner Probleme with my IDE Code::Blocks C++ 5 228
Adjust Mfcapp 29 155
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now