I am trying to read a string from a text file, where do I put my text file so that I can getline a string from it?
ifstream file ("wages.txt");
string line;
getline(file, line);
cout << line << endl;
I am trying to read a string from a text file, where do I put my text file so that I can getline a string from it?
ifstream file ("wages.txt");
string line;
getline(file, line);
cout << line << endl;
Are you at all familiar with the command line console?
Are you on Windows, Linux, whatever?
OK, I thought about it some more, and you don't really need to use the command line.
Use File Explorer to browse to the folder (directory) in which your solution (.sln) file is. You specified this when you created the project. If you're building for x64-Release, then browse further into the x64 folder then into the Release folder. Create your text file there.
I put the text file in the same directory as the (.sln) file but it still does not work
That's because you misread my reply.
Are you targeting Release build for x64?
Simplest is if you provide a full path to the file.
eg
ifstream file ("C:\\some\\path\\with\\wages.txt");
will open the file at C:\some\path\with\wages.txt . Note the double back-slashes, they are needed because C++ uses \ as escape character for \n , \t etc. The \\ inside a string gets converted to a single \ . An alternative is to use forward slashes, ie "C:/some/path/with/wages.txt", C++ leaves the / untouched.
Finally, in your example you (try to) open a file. Please remember that any file you open must at the end also get closed again. You can do that by leaving the scope with the ifstream, or by explicit closing. See also https://stackoverflow.com/questions/748014/do-i-need-to-manually-close-an-ifstream
In VS imho you can just put the filename without full path there, if the file is in the project folder.
Crealysm game & engine development: http://www.crealysm.com
Looking for a passionate, disciplined and structured producer? PM me