I am having a major problem with a really simple File I/O operation. I have a pretty simple datafile filled with a list of names and courses.

Happy Singing
Dopey Dancing
Sneezy Etiquette
Slimey Math
Grumpy Mining
Slinky Karate
Happy Mining
Dopey Mining
Doc Singing
Doc Math
Doc Medicine
Grumpy Medicine
Sleazy Karate
Stringbean Dancing
Stinky Etiquette
Stinky Dancing
Stinky Math
Silly Minging
Sleepy Karate
Sleazy Mining
Sleepy Dancing
Bashful Dancing
Bashful Singing
Grumpy Etiquette
Stringbean Writing
Dopey Writing
Doc Writing
Bashful Karate
Sneezy Medicine
Sneezy Math
Sneezy SinginG
Stinky Mining
Sloppy Etiquette
Slimy Writing
Sloppy Karate
Sloppy Math

I am trying to read the name and the course into a large MultiLinkedList. However, for some reason the data is getting corrupted between the file reading and then the input into the program. This is what the file reading operation is producing for output when I decided to do a dump of the input into a output during debugging.

happy singing਍ 搀漀瀀攀礀ഀ
dancing ਍猀渀攀攀稀礀 攀琀椀焀甀攀琀琀攀ഀ
 slimey਍洀愀琀栀 ഀ
grumpy mining਍ 猀氀椀渀欀礀ഀ
karate ਍栀愀瀀瀀礀 洀椀渀椀渀最ഀ
 dopey਍洀椀渀椀渀最 ഀ
doc singing਍ 搀漀挀ഀ
math ਍搀漀挀 洀攀搀椀挀椀渀攀ഀ
 grumpy਍洀攀搀椀挀椀渀攀 ഀ
sleazy karate਍ 猀琀爀椀渀最戀攀愀渀ഀ
dancing ਍猀琀椀渀欀礀 攀琀椀焀甀攀琀琀攀ഀ
 stinky਍搀愀渀挀椀渀最 ഀ
stinky math਍ 猀椀氀氀礀ഀ
minging ਍猀氀攀攀瀀礀 欀愀爀愀琀攀ഀ
 sleazy਍洀椀渀椀渀最 ഀ
sleepy dancing਍ 戀愀猀栀昀甀氀ഀ
dancing ਍戀愀猀栀昀甀氀 猀椀渀最椀渀最ഀ
 grumpy਍攀琀椀焀甀攀琀琀攀 ഀ
stringbean writing਍ 搀漀瀀攀礀ഀ
writing ਍搀漀挀 眀爀椀琀椀渀最ഀ
 bashful਍欀愀爀愀琀攀 ഀ
sneezy medicine਍ 猀渀攀攀稀礀ഀ
math ਍猀渀攀攀稀礀 猀椀渀最椀渀最ഀ
 stinky਍洀椀渀椀渀最 ഀ
sloppy etiquette਍ 猀氀椀洀礀ഀ
writing ਍猀氀漀瀀瀀礀 欀愀爀愀琀攀ഀ
 sloppy਍洀愀琀栀 猀氀漀瀀瀀礀ഀ

Obviously this is entirely throwing the data structure completely out whack as it has no way for handling borked input like that. Here is the entire file read operation, it is really simple...

ifstream fin("students.txt");
	ofstream fout("students_out.txt");

	if(!fin)
	{
		cout << "[ERR] - Unable to open datafile for read operations. Exiting." << endl;
		exit(1);
	}

	while (!fin.eof())
	{
		fin >> name;
		fin >> course;
		strlow(name);
		strlow(course);

		// DEBUG //
		fout << name;
		fout << " ";
		fout << course;
		fout << endl;
		// DEBUG //

		schedule.insert(name, course);
	}

	fin.close();
	fout.close();

The code for strlow() does not appear to be the problem. But if anyone wants to see it, here it goes:

void strlow(string& value)
{
	for (unsigned int i = 0; i < value.length(); ++i)
	{
		value[i] = tolower(value[i]);
	}
}

Any assistance would be appreciated, as this is the last serious bug holding up my project. I know everything else works because I can manually enter all the data from the program. But that's rather useless.

Recommended Answers

All 5 Replies

output name and course before sending them to strlow to be sure they have been read in correctly. If not, can you read the file called students when you open it in a text editor? If not how is the file called students created?

output name and course before sending them to strlow to be sure they have been read in correctly. If not, can you read the file called students when you open it in a text editor? If not how is the file called students created?

I did a cout of the contents of name and course right after they are read in and it comes out with an extra space between each character. I can read the file students.txt when I open up the file through a text editor. I commented out strlow(string& value) as well to check to see if that was the issue, and it is not.

I don't know what was wrong with the file. But I decided to go and delete the file and replace it with another filled with the same data and the program is working perfectly.

Wierd.

That's wierd. There should be no spaces in a string read in with the >> operotor as any whitespace terminates the input into the variable.

I doubt it is the problem, but this:
while (!fin.eof())
is a bug waiting to bite you. Don't use the return value of eof() to control a loop. Change it to this:

while(fin >> name)
{
     fin >> course;

If necessary I'd create another file called practic.txt using a text editor and entering the following data directly into the practice.txt using the keyboard:
first course
second class
Then I'd save practice.txt to the same folder the program is in.
Then I'd run the program associating practice.txt with fin instead of students.txt and outputting the variables name and course as they are read in to see what happens. If they are read in correctly then it's likely students.txt is the problem.

Edit:: Good for you. See my other suggestion above.

Thanks for the suggestion Lerner. I will try it out in my spare time and see how it goes.

Also I am curious why is using the return value of eof() a bug waiting to happen?

Be a part of the DaniWeb community

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