File I/O input being misread?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 16
Reputation: meddlepal is an unknown quantity at this point 
Solved Threads: 0
meddlepal meddlepal is offline Offline
Newbie Poster

File I/O input being misread?

 
0
  #1
Nov 11th, 2008
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.

  1. Happy Singing
  2. Dopey Dancing
  3. Sneezy Etiquette
  4. Slimey Math
  5. Grumpy Mining
  6. Slinky Karate
  7. Happy Mining
  8. Dopey Mining
  9. Doc Singing
  10. Doc Math
  11. Doc Medicine
  12. Grumpy Medicine
  13. Sleazy Karate
  14. Stringbean Dancing
  15. Stinky Etiquette
  16. Stinky Dancing
  17. Stinky Math
  18. Silly Minging
  19. Sleepy Karate
  20. Sleazy Mining
  21. Sleepy Dancing
  22. Bashful Dancing
  23. Bashful Singing
  24. Grumpy Etiquette
  25. Stringbean Writing
  26. Dopey Writing
  27. Doc Writing
  28. Bashful Karate
  29. Sneezy Medicine
  30. Sneezy Math
  31. Sneezy SinginG
  32. Stinky Mining
  33. Sloppy Etiquette
  34. Slimy Writing
  35. Sloppy Karate
  36. 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.

  1. happy singing਍ 搀漀瀀攀礀ഀ
  2. dancing ਍猀渀攀攀稀礀 攀琀椀焀甀攀琀琀攀ഀ
  3. slimey਍洀愀琀栀 ഀ
  4. grumpy mining਍ 猀氀椀渀欀礀ഀ
  5. karate ਍栀愀瀀瀀礀 洀椀渀椀渀最ഀ
  6. dopey਍洀椀渀椀渀最 ഀ
  7. doc singing਍ 搀漀挀ഀ
  8. math ਍搀漀挀 洀攀搀椀挀椀渀攀ഀ
  9. grumpy਍洀攀搀椀挀椀渀攀 ഀ
  10. sleazy karate਍ 猀琀爀椀渀最戀攀愀渀ഀ
  11. dancing ਍猀琀椀渀欀礀 攀琀椀焀甀攀琀琀攀ഀ
  12. stinky਍搀愀渀挀椀渀最 ഀ
  13. stinky math਍ 猀椀氀氀礀ഀ
  14. minging ਍猀氀攀攀瀀礀 欀愀爀愀琀攀ഀ
  15. sleazy਍洀椀渀椀渀最 ഀ
  16. sleepy dancing਍ 戀愀猀栀昀甀氀ഀ
  17. dancing ਍戀愀猀栀昀甀氀 猀椀渀最椀渀最ഀ
  18. grumpy਍攀琀椀焀甀攀琀琀攀 ഀ
  19. stringbean writing਍ 搀漀瀀攀礀ഀ
  20. writing ਍搀漀挀 眀爀椀琀椀渀最ഀ
  21. bashful਍欀愀爀愀琀攀 ഀ
  22. sneezy medicine਍ 猀渀攀攀稀礀ഀ
  23. math ਍猀渀攀攀稀礀 猀椀渀最椀渀最ഀ
  24. stinky਍洀椀渀椀渀最 ഀ
  25. sloppy etiquette਍ 猀氀椀洀礀ഀ
  26. writing ਍猀氀漀瀀瀀礀 欀愀爀愀琀攀ഀ
  27. 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...

  1. ifstream fin("students.txt");
  2. ofstream fout("students_out.txt");
  3.  
  4. if(!fin)
  5. {
  6. cout << "[ERR] - Unable to open datafile for read operations. Exiting." << endl;
  7. exit(1);
  8. }
  9.  
  10. while (!fin.eof())
  11. {
  12. fin >> name;
  13. fin >> course;
  14. strlow(name);
  15. strlow(course);
  16.  
  17. // DEBUG //
  18. fout << name;
  19. fout << " ";
  20. fout << course;
  21. fout << endl;
  22. // DEBUG //
  23.  
  24. schedule.insert(name, course);
  25. }
  26.  
  27. fin.close();
  28. fout.close();

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

  1. void strlow(string& value)
  2. {
  3. for (unsigned int i = 0; i < value.length(); ++i)
  4. {
  5. value[i] = tolower(value[i]);
  6. }
  7. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: File I/O input being misread?

 
0
  #2
Nov 11th, 2008
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?
Last edited by Lerner; Nov 11th, 2008 at 9:22 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: meddlepal is an unknown quantity at this point 
Solved Threads: 0
meddlepal meddlepal is offline Offline
Newbie Poster

Re: File I/O input being misread?

 
0
  #3
Nov 11th, 2008
Originally Posted by Lerner View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: meddlepal is an unknown quantity at this point 
Solved Threads: 0
meddlepal meddlepal is offline Offline
Newbie Poster

Re: File I/O input being misread?

 
0
  #4
Nov 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: File I/O input being misread?

 
0
  #5
Nov 11th, 2008
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:
  1. while(fin >> name)
  2. {
  3. 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.
Last edited by Lerner; Nov 11th, 2008 at 9:51 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: meddlepal is an unknown quantity at this point 
Solved Threads: 0
meddlepal meddlepal is offline Offline
Newbie Poster

Re: File I/O input being misread?

 
0
  #6
Nov 12th, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 506 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC