file appending

Feb 26, 2012 at 7:24am
hello i started to learn input and output with files. but when i try to open a file it doesn't work. ive just took an example from the documentation. can someone tell me how to fix this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
   ofstream example ("ReadMe.txt",ios::app);
   if(example.is_open())
   {
   example << "something";
   example.close();
   }
   else
   {cout<<"error"<<endl;}
   system("pause");
   return 0;
}


edit: he can't open the file.
maybe i have to save it on a special place or maybe i should tell him the location of the file
Last edited on Feb 26, 2012 at 7:59am
Feb 26, 2012 at 7:33am
What does it do?
Feb 26, 2012 at 7:39am
the output is error
and it is supposed to append 'something' to the ReadMe.txt file
Last edited on Feb 26, 2012 at 7:43am
Feb 26, 2012 at 7:49am
Oooh, file name is an issue I think
Feb 26, 2012 at 7:51am
you think i named the readme.txt wrong or ?
Feb 26, 2012 at 7:58am
Yea I'd imagine readme.txt is bad. There is likely a readme.txt somewhere near the compiler with no write privelages. Try a different name and see what happens
Feb 26, 2012 at 8:02am
Ok so after looking at VS2010, a ReadMe.txt gets created for each project you make. I didnt look at my code::blocks but I imagine its similar
Feb 26, 2012 at 9:11am
does it work for you ? maybe its just the compiler
Feb 26, 2012 at 5:21pm
It works for me without any changes. Try with the full path to the file, I expected it to be in the Debug folder(with executable) but it was created in a sub-folder. Executable is in C:\Users\Sean\Documents\Visual Studio 2010\Projects\Console App\Debug text file is in C:\Users\Sean\Documents\Visual Studio 2010\Projects\Console App\Console App\Debug.
Topic archived. No new replies allowed.