| | |
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:
Solved Threads: 0
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.
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.
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...
The code for strlow() does not appear to be the problem. But if anyone wants to see it, here it goes:
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.
text Syntax (Toggle Plain Text)
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.
text Syntax (Toggle Plain Text)
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...
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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:
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.
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:
C++ Syntax (Toggle Plain Text)
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.
Last edited by Lerner; Nov 11th, 2008 at 9:51 pm.
Klatu Barada Nikto
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: How do I Stop the program form listing empty variables?
- Next Thread: can anyone help me solving this program ??plz
Views: 506 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






