![]() |
| ||
| Reading txt file into Hash Table I have this program that is supposed to read part numbers from a text file. 101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far :confused: Any suggestions are appreciated. #include <iostream.h> |
| ||
| Re: Reading txt file into Hash Table I am not quite sure what your part.txt file looks like, but you have to tell your for loop when the file ends, or it will just keep going 40 times. |
| ||
| Re: Reading txt file into Hash Table The best way to read from a file is to use the input gathering function itself as your loop condition. That way when it returns a failure code (supposedly for reaching end-of-file), you break the loop: while (inputfile >> PartNumber) {I removed your comments because they do not add anything. Comments that say the same thing as the code being commented only serve to make the program harder to read. There's also a good chance that any changes to the code will not be reflected in the comment and the two will disagree. When code and comments disagree, it's customary to see both as incorrect.After the loop, it's also a good idea to make sure that it was end-of-file that caused it to terminate. That way you can handle real errors: while (inputfile >> PartNumber) { |
| ||
| Re: Reading txt file into Hash Table thanks - I was thinking about it after - and I am only entering 30 part numbers - and the array holds 41 elements...I know its not the best way to do it...but I just changed the value to 30. I think there must be a fancy way to put something in there to know its at the end of the file. But I will worry about that another time. |
| ||
| Re: Reading txt file into Hash Table Quote:
|
| ||
| Re: Reading txt file into Hash Table Quote:
This is because when you are reading the numbers from the file you are trying to read 41 characters..but only 31 characters are present in the file.. so it is reading the last cahracter 10 more times.Try to decrease the value of i in the for loop and you will get your desired result. |
| ||
| new to c++ i just need to know what does this line mean? and why its not working with my compiler? it is giving an error that type qualifier 'std' must be a struct or class name std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary); if(!inputfile) { std::cout<<"Could not open file"<<std::endl; return 1; |
| ||
| The recommended scheme for File I/O is to create an fstream object, attempt to open a file, perform error checking, and load the file. The code in question is performing error checking to see if the file was actually opened. It is quite possible that the file may have been moved, deleted, renamed.. or was just never created: #include<fstream> A slight variation involves the use of the ifstream constructor: ifstream infile("C:\\Users\\Dave\\Documents\\test.txt"); |
| ||
| for(i = 0; i < 41; i++) { // as you read each part number in the file inputfile >> PartNumber; // HashAddress function returns array index and it is assigned to variable Index I am getting an error on this line. Isn't inputfile the part of any of the headers? C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : error C2065: 'inputfile' : undeclared identifier C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : warning C4552: '>>' : operator has no effect; expected operator with side-effect |
| All times are GMT -4. The time now is 11:46 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC