User input of file name to open

Have a project I'm working on in my free time offshore that I'm sure I'll be here quite often.

I can get access to the file and have it print to the console if I hard code in the directory of the file. How would I go about using user input to open a file in a specified directory I'm already pointing to?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
 
int main()
  {
    string filename;
    
    cout << "Enter the file name with the .csv extension: ";
      getline(cin, filename);
    
    ifstream myfile;
    
      myfile.open ("C:\\Program Files\\DataLog Express\\"user defined filename here"");
         while (myfile.good())
           cout << (char) myfile.get();
   }
After getting the filename from the user,
1
2
3
4
5
6
    string path = "C:\\Program Files\\DataLog Express\\";
    string fullname = path + filename;
    ifstream myfile(fullname.c_str());
    char ch;
    while (myfile >> ch)
        cout << ch;

Thanks Chervil. Now I'll work on marking it as solved.
Topic archived. No new replies allowed.