(solved)fstream - name with space

Hello

I'm trying to write simple program that reads stuff from files and write it to other file, there is no problem here.
However when i try to open file with space in name or directory it just won't work. e.g "C:\Program Files\filename.txt" "C:\file name.txt"

This is what I use to check if file exist and similar to actually open file.

1
2
3
4
5
bool fexists(string filename)
{
    ifstream ifile(filename.c_str());
    return ifile;
}


Is there way to make this work? like symbol or string that should replace white space with before using in ifstream ?
Last edited on
I made a quick program that opened a file called "file file.txt" and wrote to it. It worked fine. Perhaps there is a typo in your code.
My bad, looks like I didn't check it properly when you type in file name that is in the same directory as executable then indeed it works, however it does not if you type whole directory and in either file name or directory is space somewhere.
In a literal string the '\' character is interpreted as an escape code to allow you to embed characters that otherwise cannot be entered or are nonprintable. For example to embed a newline in a literal string you cannot just hit the Enter key while typing the literal string because the editor would respond by actually starting a new line. So instead you type "\n", i.e., "This is on the first line\nThis is on the second line". To enter a '\' character you need to escape it by entering two slashes. The first slash is the escape character and the second slash is the slash character being embedded.

"C:\\Program Files\\filename.txt"
Last edited on
Indeed when I doubled slashes AND removed " from begging and ending of name string that were automatically added when drag and doping into program window it worked as intended.

That solves my problem. Thank you for help.
Topic archived. No new replies allowed.