New course! Every coder should learn Generative AI!
Try a free lesson0
Fstream, ofstream, ifstream
Hi class. If we use sololearn compiler to create, write or read sequential file (for example clients.dat, clients.txt) associated or assigned to ofstream object outClientfile, where do I can find the output file clients.txt or clients.dat in my computer. i could not find the output file, event I have determined the output file .txt or .dat directory by add "c:/clients.txt" in my code. Thank you.
1 Answer
+ 1
If you don't specify directory, the file path should be relative to either a setting in your IDE, the project's directory or the directory containing the generated exe file.
Maybe you ran into a permission issue. It is good to work on your project in a directory that doesn't have special permission requirements for reading and writing. Somewhere on the Desktop should be safe. Anywhere in Program Files or Windows directory would be bad.
Write a simple program that does nothing but write a file with a very unique name like "sdgs8dgs8ygds8gsdg8ysd_.txt" and check for any file open, permission errors...
2. Run it.
3. Search your entire file system in all drives for "sdgs8dgs8ygds8gsdg8ysd_.txt".
It would be very weird if your program ran without throwing an exception or exiting with error code yet the file wasn't written somewhere.
Here's some code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("sdgs8dgs8ygds8gsdg8ysd_.txt");
if (!myfile) {
cout << "Unable to write to file because of an error. This could be related to permissions" << endl;
}
else {
myfile << "Writing this to a file.\n";
myfile.close();
}
return 0;
}