Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to write file in C++

I'm trying to to the most basic of things .... write a file in C++, but the file is not being written. I don't get any errors either. Maybe I'm missing something obvious ... or what?

I thought there was something wrong with my code, but I also tried a sample I found on the net and still no file is created.

This is the code:

ofstream myfile;
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt");
myfile << "Writing this to a file.\n";
myfile.close();

I've also tried creating the file manually beforehand, but it's not updated at all.

I'm running Windows 7 64bit if that has got something to do with this. It's like file-write operations are completely forbidden and no error messages or exceptions are shown.

like image 660
Thorgeir Avatar asked Oct 12 '09 22:10

Thorgeir


People also ask

What does Cannot write to file?

An output file cannot be opened for writing. The file (or the folder containing the file) may be opened for exclusive use by another process, or it may have its read-only attribute set.

Which command is used for write in file in C?

Writing to a file (fprintf or fputs) Moving to a specific location in a file (fseek, rewind) Closing a file (fclose)


3 Answers

You need to open the file in write mode:

myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt", ios::out);

Make sure to look at the other options for that second argument, as well. If you're writing binary data you'll need ios::binary for example.

You should also be checking the stream after opening it:

myfile.open(...
if (myfile.is_open())
    ...

Update:

AraK is right, I forgot that an ofstream is in write mode by default, so that's not the problem.

Perhaps you simply don't have write/create permissions to the directory? Win7 defaults a lot of directories with special permissions of "deny all". Or perhaps that file already exists and is read-only?

like image 166
Tim Sylvester Avatar answered Sep 30 '22 06:09

Tim Sylvester


Start off by turning that slash around.
Even Windows understands the slash being the other way around.

ofstream myfile("C:/Users/Thorgeir/Documents/test.txt");

You could test if there are any errors:

if (!myfile)
{
    std::cout << "Somthing failed while opening the file\n";
}
else
{
    myfile << "Writing this to a file.\n";
    myfile.close();
}
  • Make sure the directory exists.
  • If the file exists make sure it is writeable (by you)
  • Check the directory you are writing into is writeable (by you)
like image 45
Martin York Avatar answered Sep 30 '22 08:09

Martin York


Have you read about UAC (User Account Control) and UAC Virtualization / Data Redirection in Windows Vista and 7? It's possible that your file is actually in the Virtual Store.

User Account Control Data Redirection

Your example output directory is in Users, so I wouldn't think this would be the issue, but it's a possibility worth mentioning and something that can be very frustrating if you're not looking out for it!

Hope this helps.

like image 43
nselikoff Avatar answered Sep 30 '22 08:09

nselikoff