Help opening file by path

I cannot get my file to open when I use the path:

C:\Users\Name\ClionProjects\Project\test.txt

Is it because of I'm using a string? Otherwise I am at a loss.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

    string filename;
    cout << "Enter the name of the data file to open with the extension" << endl;
    getline(cin,filename);

    ifstream input_file(filename.c_str());

    if (!input_file) { //if file is not open
        cout << "not open";
        }
    else {
        cout << "open";
    }
Last edited on
closed account (E0p9LyTq)
Because of how the compiler parses string literals containing the \' (backslash) character you should either:

1. double up the '\' character: "C:\\Users\\Name\\ClionProjects\\Project\\test.txt"

2. use the '/' (forward slash) character instead: "C:/Users/Name/ClionProjects/Project/test.txt"

http://stackoverflow.com/questions/10220401/c-string-literals-escape-character
Topic archived. No new replies allowed.