0

Hey Everyone,

I'm compiling a code that reads characters from a file and puts them in a linked list. No problem with the Linked List but my ifstream acts weird. When it reads from the file everything goes somoothly, but at the end, when it reads that last WORD, it reads it twice. For example, I have a file with "I'm trying to figure this out here." written inside, when I used the following code to read the contents, the out put is:

"I'm trying to figure this out here. here.".

Any idea???

Here is the code:

void load()
{
	clscr();
	string answer;
	cout << "Command: ";
	cin >> answer;

	/* Try to open the file */
	ifstream fin;
	fin.open(answer.c_str());
	string temp;

	Lines *newNode = new Lines;
	Lines *cur;

	while( !fin.eof() ) {
		fin >> temp;
                cout << temp; //Here seems the problem exists. One extra loop!!
		if (lineHead == NULL) {
			newNode->next = lineHead;
			lineHead = newNode;
		}
		else {
			cur = newNode;
			newNode = new Lines;
			newNode->next = NULL;
			cur->next = newNode;
		}
		newNode->line += temp;
	}
	cout << endl;
	fin.close();
}

Thanks.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by group256
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.