How to use a user entered file in <fstream>?

So, I am making a name-database-type-thing and I want the user to be able to enter there own file. Like, here:
What is your account file name?

And then I have this (code):
1
2
3
4
5
6
7
8
9
10
string fileName;
string name;

cout<<"What is your file name?"<<endl;
getline(cin, fileName);

ifstream nameFile(fileName);

getline(cin, name);
nameFile<<"Name: "<<endl;


Well, that last part is in a loop, so you can enter an unlimited number of names to the .txt file.

So, it won't let me use the string in ifstream. How can I fix this? This maybe unclear, so if it is just say so and I will edit it.

Thanks
If your ifstream constructor doesn't allow std::strings as a parameter, try using the c_str() method.
If your ifstream constructor doesn't allow std::strings as a parameter, try to update your compiler.
Make sure that you are using the c++11 standard (for g++, is the `-std=c++11' flag)
and ifstream is for reading from a file.

What you want is an ofstream, so you can write to the file.

your code should work once you change ifstream to ofstream.
Ok, I got it to where it had no errors, and I got it running. When asked the file name, I typed sal.txt. Although when the program ends, I opened the projects file location and there was no file named 'sal' in the folder. Is there something I did wrong?
Last edited on
I'd make sure that folder is actually considered the working directory for when you run the executable. You could try putting in an absolute path also.
As in absolute path... you mean in the project's file?
Topic archived. No new replies allowed.