Directory from file...
I have a file with a directory (C:\my programs) in a file and I want to turn this into "C:\\my programs" so that I can give it into another "fopen".
I use C++.
Just scan back along the string to find a backslash (or forward slash), then trim of everything after that.
Also, you don't need to escape your backslashes in a string unless you're writing it directly into the code.
Also, you don't need to escape your backslashes in a string unless you're writing it directly into the code.
Are you saying you have a text file that contains the string "C:\my programs"? If so, you don't need to do anything. The double slashes are required to represent a single slash to allow string literals in C++ to support escape sequences for (usually) whitespace characters.
Example:
Example:
#include <iostream>int main(){ std::cout << "Count the number of slashes: C:\\Program Files\\foo\\bar\n";}
Quote:Original post by Evil Steve
Just scan back along the string to find a backslash (or forward slash), then trim of everything after that.
Also, you don't need to escape your backslashes in a string unless you're writing it directly into the code.
That's what I am doing...
I'm writing it directly into the code...
Quote:Original post by KontosHarryThen you just put another backslash before every backslash in the path...
That's what I am doing...
I'm writing it directly into the code...
Then you can take it directly as the user types it in. The Escape sequences are only needed inside your source-code.
edit: But when I look back at your posts, I probably have misunderstood you :/
int main () { { string something ("C:\\my programs\\"); FILE *f = fopen (something.str()+"sometextfile.txt", "r"); ... fclose (f); // btw: better use C++ functionality } { string something; ... // get something from user via commandline or somehow // nothing to do here, user can type single backslahes FILE *f = fopen (something.str()+"sometextfile.txt", "r"); ... fclose (f); // btw: better use C++ functionality }}
edit: But when I look back at your posts, I probably have misunderstood you :/
Let's make sure we have this right.
You have a file name which is written in your source code.
The named file contains text which gives the path to another file.
You want to open that other file.
Yes?
You have a file name which is written in your source code.
The named file contains text which gives the path to another file.
You want to open that other file.
Yes?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement