Why can't I write/read from a file?

I'm afraid I can't see what's wrong with my code and would very much appreciate any help anyone can give me. I want to be able to write a bunch of lines to a file, go back to the beginning and read line after line in the file until I spot something I want to replace and then replace what's there.

All I'm trying to do right now is be able to read and write to the file in a single program, but it's not happening. Can anyone smarter than me explain why and help me out here?

Much appreciated guys.








#include <iostream>
#include <vector>
#include <fstream>

int mian()

{

fstream filestr ("afile.txt", fstream::in | fstream::out);
string line;

filestr.open("afile.txt");
filestr<<"first line"<<endl<<"second line"<<endl<<"third"<<endl;
filestr.close();
filestr.open("afile.txt");

while (filestr.good())
{
getline(filestr, line);
cout<<line<<"hello"<<endl;
}

system("pause");
return 0;

}
Please can someone help?
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
31
32
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()

{


fstream filestr;
filestr.open ("file.txt", fstream::out);

string line;


filestr<<"first line"<<endl<<"second line"<<endl<<"third"<<endl;
filestr.close();


filestr.open ("file.txt", fstream::in);
 while (filestr.good())
 {
 getline(filestr, line);
 cout<<line<<"hello"<<endl;
 }


return 0;

}
Topic archived. No new replies allowed.