Reading a file into a Parallel Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 1
Reputation: nhenson is an unknown quantity at this point 
Solved Threads: 0
nhenson nhenson is offline Offline
Newbie Poster

Reading a file into a Parallel Array

 
0
  #1
Sep 30th, 2007
I have been working on this problem since last week and I about to go insane. I got it working and then all of the sudden it got screwed up. I want to know if someone can help me find out what I am doing wrong to fix it again.
Here's an example of what I have to read in.
K G Johnson 4598 85 95 72 50

Here is a the piece of my code that has me baffled:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int classid[4], test1[3], test2[3], test3[3], final[3], i, ;
  9. char name[9][14];
  10.  
  11. for(i=0; i<=8; i++)name[i][13]='\0';
  12. //Open file...
  13. ifstream classrec;
  14. classrec.open("STUDNTRECS.TXT");
  15. ofstream output;
  16. output.open("STUDENTRECSOUT.TXT");
  17. //Read file and calculate the letter grade of each student...
  18. for(i=0; i<8; i++)
  19. {
  20. classrec.get(name[i],14);
  21. classrec>>classid[i]>>test1[i]>>test2[i]>>test3[i]>>final[i]>>ws;
  22.  
  23.  
  24. output<<name[i]<<" "<<classid[i]<<" "<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<final[i]<<endl;
  25. }
When I read in the file my and send it to the output file it is usually the right information, but it ends up in the wrong places. I get numbers from the class id in the character information. I have come here as a last resort to understand what it is I messed up. Please help.
Last edited by Ancient Dragon; Sep 30th, 2007 at 11:27 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading a file into a Parallel Array

 
0
  #2
Sep 30th, 2007
The get() function will get exactly the number of characters you specify unless it first encounters the '\n' in the file or end-of-file. Line 20 is asking for 14 characters and that's exactly what you'll get. In the example line you posted it will read "K G Johnson 45", which is probably not what you intended. I think a better way of reading that file is to first read it one character at a time until the first numeric character is encountered. This code too has a couple of minor problems but probably ok for your program.
  1. int count = 0;
  2. int c;
  3. while( !isdigit((c = cin.get()) )
  4. {
  5. name[i][count] = c;
  6. ++count;
  7. }
  8. name[i][count] = 0; // null-terminate the string

The size of classid is one character too small -- you have to make room for the null terminating byte.
Last edited by Ancient Dragon; Sep 30th, 2007 at 11:45 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC