Sign in to follow this  
KontosHarry

Directory from file...

Recommended Posts

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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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";
}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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 :/

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this