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()
	string answer;
	cout << "Command: ";
	cin >> answer;

	/* Try to open the file */
	ifstream fin;
	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;


Amzing Jonsca. Good lesson. Thanks.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.