At the end of a simulation, I want to write some results as an appended row to a data file. The code I am using is the following, where you can assume that
outFile was correctly allocated as an
output_file is a
std::string containing a valid path to a file that does not yet exist, and that the variables printed out to the file are just
int types that get values during the simulation.
outFile.open(output_file.c_str(), std::ios::out | std::ios::app ); outFile << num_nodes << ", " << tot_s << ", " << tot_c << ", " << tot_d << std::endl; outFile.close();
I've checked whether it correctly opens the file with the
ofstream::is_open() function and it returns
false. However, I can't figure out why. I've tried it with many different file names and directory paths, all of which I have checked and they are valid (no typos, etc.)
The file being written is just into a folder on the desktop where I create files all the time, so I don't see how it could be a permissions issue. If it was a permissions issue, how can I check that?
Otherwise, what else can be preventing it from writing to the file?
Following up on the comments, after adding a call to
perror(), it is displaying the "No such file or directory" error. The file path in question is:
I want this file to be created, and all the directories in that path exist, it's all spelled correctly, etc., and there are no weird permission issues with the GamesOnCycle folder or its data subfolder. Note that it is a linux system (Ubuntu 11.04) so the forward slashes are correct for the file path, unless I'm missing something that C++ has to have w.r.t. file paths.
std::stringat least - ely 2012-04-04 21:03
output_file.c_str()around the time of opening? It's possible that there was an unanticipated change - chris 2012-04-04 21:05
ofstreamwould create directories if any did not exist - hmjd 2012-04-04 21:08
std::string output_file = ...at the moment the string gets assigned from user input, but I already declared the variable
std::string output_fileprior to that. Somehow the compiler didn't complain about re-declaring and somehow this led to an empty file name - ely 2012-04-04 21:12
::output_file. Glad to help pinpoint the problem. Debugging output sometimes comes in pretty handy : - chris 2012-04-04 21:15
std::string::assignmethod. Now fixed - ely 2012-04-04 21:15
main()and not inside of anything else - ely 2012-04-04 21:16
perror()before, though, and that was really what helped illustrate the problem - ely 2012-04-04 21:19
This could be happening due to several reasons.
1) The file is already open.
2) All the directories in the file path are not created.
3) Lack of file permissions.
For an additional reference, please see When will ofstream::open fail?
This may sound bad, but are you on windows or linux? If windows, for your file path, do you have it defined with double "\" in your string, or just one? If just one, you aren't putting the characters in your path that you think you are. To be safe, use the "/" character.
So if you had this:
string pathname = "C:\Users\me\Desktop";
That is NOT a valid path. You are escaping "\U", "\m" and "\D" into your string. You'd need this:
string pathname = "C:\\Users\\me\\Desktop";
string pathname = "C:/Users/me/Desktop";
The "/" isn't an escape character.
It's what seems likely to me.