Directory from file...

Started by
7 comments, last by KontosHarry 15 years, 1 month ago
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++.
Advertisement
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.
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:
#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 KontosHarry
That's what I am doing...

I'm writing it directly into the code...
Then you just put another backslash before every backslash in the path...
I mean that the file will be used by the user to set the directory...
Then you can take it directly as the user types it in. The Escape sequences are only needed inside your source-code.

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?
Quote:Original post by Zahlman
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?


Yeah that's right...

This topic is closed to new replies.

Advertisement