Reading from a file on a specific location

How do you read from a file and give a specific location? I know there's the Constructor for fstream and the open() function. And in most tutorials I've seen (including the ones on this site) just write in the filename in the open() function, not it's location.

Lets assume we have two files named "rt.txt" in C:\Random and C:\MyTxtFiles
If you use open("rt.txt"); what file will it read from? How do you read from the file in C:\Random?
I tried open("C:\Random\rt.txt"); but it didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
    string line;
    fstream rtFile;
    rtFile.open("C:\Random\rt.txt"); // I have a .txt file on this location

    if (rtFile.is_open())
    {
        while (getline(rtFile, line))
        {
            cout << line << endl;
        }


    } else cout <<"Error! File is not open! ";
Last edited on
\ is the escape character. Either "C:\\Random\\rt.txt" or "C:/Random/rt.txt".
But of course! How did I manage to forget that!

Thank you very much! Both work.
Topic archived. No new replies allowed.