fstream problem

This little program is intended to write the following text in a .txt file.

Hello (This is first line)
0123456789 (This is second line)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream file;

file.open("c:\\test.txt",ios_base::out);

if (!file)
cerr<<"Error"<<endl;
else
{
file<<"Hello"<<endl;
file.close();
cout<<"End"<<endl;
}

file.open("c:\\test.txt",ios_base::app); 

if (!file)
cerr<<"Error"<<endl;
else
{
for (int i=0;i<10;i++)
file<<i;
cout<<"End"<<endl;
}
}


After sucessfully completed the first part(Hello), it shows Error(designed in Line 23) when coming to the second part. It cannot perform the write-in of 0123456789. What is the problem? Thank you.
I'm not sure if ios_base::app on it's own is enough.

Try file.open("c:\\test.txt",ios_base::out|ios_base::app);
Thank you. Your suggestion worked.
Topic archived. No new replies allowed.