File Input & Output

This is the code I have so far for this question...

Write a C++ program to read information of eight countries from an input file, called countries.txt. Write this information to an output file, called report.txt, in the format specified below.

Output format is
Statscan EDI
Code Code Country
======== ==== =======
12345 AB Cname

countries.txt is...

03461
Australia
AU
4023
Bahamas
BS
80021
Canada
CA
18712
Ethiopia
ET
20251
France
FR
34041
Lebanon
LB
38822
Mexico
MX
66721
United States
US


I have this much coded so far, but can't seem to understand these errors.

(5) : error C2365: 'std::getline' : redefinition; previous definition was 'function'
(5) : fatal error C1903: unable to recover from previous error(s); stopping compilation




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>
#include<string>
using namespace std;
int std::getline;

int main()
{
	int scancode;
	string country;
	string abrev;
	
    ifstream inData;
    ofstream outData;

	inData.open("countries.txt");
	outData.open("report.txt");

	while (!inData.eof())
	{
		inData.getline(scancode '\n');
		inData.getline(country, '\n');
		inData.getline(abrev, '\n');
		cout << scancode << endl;
	}
	inData.close();
  
outData << 
	return 0;
}
Remove line 5.
I tried that, but then I get these errors.

(21) : error C2143: syntax error : missing ')' before 'constant'

(21) : error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

(21) : error C2059: syntax error : ')'

(22) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

(23) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::string' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

(29) : error C2059: syntax error : 'return'

http://www.cplusplus.com/reference/iostream/istream/getline/

(29) : error C2059: syntax error : 'return'
See line 28.
Okay, I havn't finished exactly with my output as of yet, but I still am unsure about the other errors. Thanks for your 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
33
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// int std::getline; // error // solution delete

int main()
{
	int scancode;
	string country; // error should be char[] array
	string abrev; // error should be char[] array
	
    ifstream inData;
    ofstream outData;

	inData.open("countries.txt");
	outData.open("report.txt");

	while (!inData.eof())
	{
		inData.getline(scancode '\n'); // error missing comma and '\n' should be number
                // solution inData >> scancode;
		inData.getline(country, '\n'); // error require char[] array , '\n' should be number
                // example inData.getline(country , sizeof(country))
		inData.getline(abrev, '\n'); // error require char[] array , '\n' should be number
		cout << scancode << endl;
	}
	inData.close();
  
outData << // error data is unfinish
// error outData is not close
	return 0;
}
Last edited on
Tried this...

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


int main()
{
int scancode;
char country[8];
char abrev[8];

ifstream inData;
ofstream outData;

inData.open("countries.txt");
outData.open("report.txt");

while (!inData.eof())
{
inData >> scancode;
inData.getline(country ,sizeof(country));
inData.getline(abrev,sizeof(abrev));
}
inData.close();

outData << scancode << endl;
outData << country(sizeof) << endl;
outData << abrev(sizeof) << endl;
inData.close();
return 0;
}

Get these errors now...

(28) : error C2059: syntax error : ')'
(29) : error C2059: syntax error : ')'

Thank you for all your help!

no sizeof needed here
1
2
outData << country << endl;
outData << abrev << endl;


still i don't see outData.close();
Last edited on
okay, changed that and added the outData.close(); but now it won't send any of the information to the report file.
make char array are long enough to store names plus null char.

1
2
3
4
//if you want this to write all data to report  text must be in loop
outData << scancode << endl;
outData << country << endl;
outData << abrev << endl;

Another Option
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void strfile(string file_read)
{
	string line;
	ifstream myfile (file_read.c_str());
	if (myfile.is_open())
	{
		while (getline (myfile,line) )
		{
			cout << line << endl;
		}
		myfile.close();
	}

	else cout << "Unable to open file"; 
}

this print a space line, they say this way is wrong
1
2
3
while (! myfile.eof() )
{
      getline (myfile,line);

Last edited on
just looks like you needed a space catcher , and move your write data
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
33
34
35
36
37
38
39
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int scancode;
char country[15];
char abrev[8];
char spacecatcher[100];

ifstream inData;
ofstream outData;

inData.open("countries.txt");
outData.open("report.txt");

while (inData)
{
	inData >> scancode;
	inData.getline(spacecatcher , sizeof(spacecatcher));
	inData.getline(country ,sizeof(country));
	inData.getline(abrev,sizeof(abrev));
	if (!inData.eof()) // for you don't write or print empty space
	{
		cout << scancode << " " << country << " " << abrev << endl;

		outData << scancode << endl;
		outData << country << endl;
		outData << abrev << endl;
	}
}

inData.close();

outData.close(); 
return 0;
} 
Topic archived. No new replies allowed.