Import file reading extra line

Write your question here.

Hi, I have built a program that imports and reads a file but I am having a small problem with my code. When the file is reading the file it's not stopping at the last row and is including it in the output. Can let me know where I have gone wrong and what I can do to fix?

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
void importFile(string first[MAXCHAR])
{
	
	ifstream import;
	string filename;
	string last;
	string name;
	
	cin.ignore();
	cout << "Enter file name for import: ";
	getline(cin,filename);
	import.open(filename.c_str());
	

	for(int i=0; !import.eof(); i++)
	{
		getline(import, name);
		int pos = name.find(",");
		string last = name.substr(0,pos);
		cout << "\"" << last << "\"" << endl;

	}
	

	cout << "Import was successful" << endl;
	
	
}
Because of this !import.eof(). Do not loop on eof (unless you know how it works and it is what you want) it was not created for that. Loop on input operation:
1
2
3
4
5
while(getline(import, name)) { //While read was successfull
    int pos = name.find(",");
    string last = name.substr(0,pos);
    cout << "\"" << last << "\"" << endl;
}
Topic archived. No new replies allowed.