Get confused in the combination of ifstream and ofstream,

I wrote a codes which can store user multiple input into dat file by running it again and again and then read it from fstream file while it is available by using vector (thinking to read a line one by one and stores in vector), but it just won't work? Where did I get wrong? I'm not quite sure about the concept of the combination of ofstream and ifstream actually ...and when should i close the file if i want to read the line and then write input to next 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
31
32
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	ifstream myfile;
	vector<string>contact;
	string name;
myfile.open("test.dat");
if (myfile.is_open())
{
	while (!myfile.eof())
		{
			
			name.getline(myfile,sizeof(myfile),'\n');
			contact.push_back(name);
			cout<<"I read"<<contact[0]<<endl;	
}
}
else
{
ofstream myfile;
myfile.open("test.dat", ios:app);
if(myfile.is_open())
{
	myfile<<argv[1]<<endl;
	myfile<<"testing"<<endl;
}
}
Learn to read error messages.
main.cpp(17) : error C2039: 'getline' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
There is no function string::getline. There is however http://www.cplusplus.com/reference/string/getline/ and http://www.cplusplus.com/reference/iostream/istream/getline/
Use the first one.

main.cpp(26) : error C2275: 'std::ios' : illegal use of this type as an expression
main.cpp(26) : error C2143: syntax error : missing ')' before ':'
main.cpp(26) : error C2059: syntax error : ')'
There is something very wrong with line 26. I wonder what's a : doing there. scope operator is ::
Last edited on
Topic archived. No new replies allowed.