stringstream issues

I was wondering why this isn't working it for some reason won't read the file and I think it has to do with the stringstream giving a false file name.



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;

string userfilename;
string text;

int main()
{
cout << "Enter name of fle you would like to open: ";
cin >> userfilename;
stringstream filename;
filename << userfilename << ".txt";
ifstream filei;
filei.open (filename.str().c_str())
filei.seekg (ios::beg)
filei >> text;
}
I don't see anything wrong with that code. Make sure the file is in the current directory (if you're running the exe from an IDE like Visual Studio or Code Blocks, the current directory is typically the project folder and not the same folder that the exe is in)
Last edited on
Use the filei.is_open() function to be sure that the file is opened.
Topic archived. No new replies allowed.