C++ Reading from a text file

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

Join Date: May 2007
Posts: 7
Reputation: rockthrower is an unknown quantity at this point 
Solved Threads: 0
rockthrower rockthrower is offline Offline
Newbie Poster

C++ Reading from a text file

 
0
  #1
May 13th, 2007
Hello,

I am trying to read data from a text file in the following format:
05 mike smith 30.00 2 1

so the format is: int, character[21] = max size of array, double, int, int

I am trying to read the values into an array of objects. I can read the first int and the name fine but I can't read in the rest of the values. Heres my code:

  1. numEmp = 0;
  2. empFile >> id;
  3. empFile.ignore(); // Discard space before name
  4. empFile.getline(name, 21, '\n'); // Store up to 20 characters
  5. empFile >> rate >> dep >> type; //I think this is the problem
  6.  
  7. while (empFile && numEmp < MAX_EMP)
  8. {
  9. // Move new employee info into array and increment numEmp count
  10. Emp[numEmp].set(id, name, rate, dep, type);
  11. numEmp++;
  12.  
  13. // Get next employee info
  14. empFile >> id;
  15. outputFile << id << endl;
  16. empFile.ignore(); // Discard space before name
  17. empFile.getline(name, 21, '\n'); // Store up to 20 characters
  18. empFile >> rate >> dep >> type;
  19. }
Last edited by Ancient Dragon; May 13th, 2007 at 1:00 am. Reason: changed code tags to add line numbers
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: C++ Reading from a text file

 
0
  #2
May 13th, 2007
discard the spaces between the name/rate/dep/type
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
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: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Reading from a text file

 
0
  #3
May 13th, 2007
Line 3 can be deleted because the stream's extraction operaotr >> will ignore all white space (spaces and tabs) between words.

Line 4: you don't want to use getline() here because it reads the entire line, not just the next two words. If the name is always in the format you show then its probably better and easier to use the extractions operator >> to read the first name and last name separately.
  1. string firstName,lastName;
  2. empFile>> firstName >> lastName;
  3. // now concantinate them together
  4. string name = firstName + " " + lastName;

line 5 should work ok if you make the above changes
Last edited by Ancient Dragon; May 13th, 2007 at 1:11 am.
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  
Join Date: May 2007
Posts: 7
Reputation: rockthrower is an unknown quantity at this point 
Solved Threads: 0
rockthrower rockthrower is offline Offline
Newbie Poster

Re: C++ Reading from a text file

 
0
  #4
May 13th, 2007
Thanks for the response.

The only problem is that I need to ultimately store the name, both first and last name into a character array which is apart of a class object. I tried
  1. strcpy(name, tempName); //where tempName is a string

I get an error with that syntax; what is the correct way to copy a string into an array?

Also, is it possible to take in the entire line of input into one array and then extract the specific variables?

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 7
Reputation: rockthrower is an unknown quantity at this point 
Solved Threads: 0
rockthrower rockthrower is offline Offline
Newbie Poster

Re: C++ Reading from a text file

 
0
  #5
May 13th, 2007
I got around the issue of copying the string into an array using the following code:
  1. // tempName is a string, name is an array of characters
  2. tempName.copy(name, 21, 0);
Last edited by rockthrower; May 13th, 2007 at 5:40 pm. Reason: Formatting
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 7
Reputation: rockthrower is an unknown quantity at this point 
Solved Threads: 0
rockthrower rockthrower is offline Offline
Newbie Poster

Re: C++ Reading from a text file

 
0
  #6
May 13th, 2007
I am only able to copy in the first line of data from the text file but not the rest. For some reason, it is not looping until empFile && empNum are less than MAX_EMP:

  1. <ol style="list-style-type: decimal"><li>const int MAX_EMP = 100;</li>
  2. <li>int numEmp = 0;</li>
  3. <li>empFile >> id;</li>
  4. <li>empFile >> firstName >> lastName; // Extract first and last name</li>
  5. <li>tempName = firstName + " " + lastName; // Concantinate both names</li>
  6. <li>tempName.copy(name, 21, 0);</li>
  7. <li>empFile >> rate >> dep >> type; // Extract rest of varbiables</li>
  8. <li>while (empFile && numEmp < MAX_EMP)</li>
  9. <li>{</li>
  10. <li>// Move new employee info into array and increment numEmp count</li>
  11. <li>Emp[numEmp].set(id, name, rate, dep, type);</li>
  12. <li>numEmp++;</li>
  13. <li>// Get next employee info</li>
  14. <li>empFile >> id;</li>
  15. <li>empFile >> firstName >> lastName; // Extract first and last name</li>
  16. <li>tempName = firstName + " " + lastName; // Concantinate both names</li>
  17. <li>tempName.copy(name, 21, 0);</li>
  18. <li>empFile >> rate >> dep >> type; // Extract rest of varbiables</li>
  19. <li>}</li>
  20. </ol>
Last edited by rockthrower; May 13th, 2007 at 6:16 pm. Reason: Formatting
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: C++ Reading from a text file

 
0
  #7
May 13th, 2007
Rockthrower, don't manually use [list] bbcode to create line numbers. Instead use [code=c] or [code=cplusplus] bbcode.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 7
Reputation: rockthrower is an unknown quantity at this point 
Solved Threads: 0
rockthrower rockthrower is offline Offline
Newbie Poster

Re: C++ Reading from a text file

 
0
  #8
May 13th, 2007
I realized that it was not looping because i was not taking in the very last character in the line. I initialized a new variable to take in the last character and although it loops now, it still does not loop through the entire file nor does it read the correct numbers after the double amount. Below is a sample of the text file I am reading in:

5 Christine Kim 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F
17 Juan Gonzales 30.00 1 1 M
18 Morris Kramer 8.95 0 0 M

The first integar, the name and the double amount are stored correctly but the last two integars and the last character are not stored correctly. My revised code is:

  1.  
  2. numEmp = 0;
  3. empFile >> id;
  4. empFile >> firstName >> lastName; // Extract first and last name
  5. tempName = firstName + " " + lastName; // Concantinate both names
  6. tempName.copy(name, 21, 0); // Copy string into array called name
  7. empFile >> rate >> type >> sex; // Extract the rest of the variables in line
  8.  
  9. while (empFile && numEmp < MAX_EMP)
  10. {
  11. // Move new employee info into array and increment numEmp count
  12. Emp[numEmp].set(id, name, rate, dep, type);
  13. numEmp++;
  14.  
  15. // Get next employee info
  16. empFile >> id;
  17. empFile >> firstName >> lastName; // Extract first and last name
  18. tempName = firstName + " " + lastName; // Concantinate both names
  19. tempName.copy(name, 21, 0); // Copy string into array called name
  20. empFile >> rate >> type >> sex; // Extract the rest of the variables in line
  21. }
Last edited by rockthrower; May 13th, 2007 at 7:05 pm. Reason: formatting
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
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: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Reading from a text file

 
0
  #9
May 13th, 2007
does Emp.set() copy name into another class variable? If it does then you don't neet 6 or 19, but you can use string's c_str() method instead to pass a char* to that function, like this:
Emp[numEmp].set(id, tempName.c_str(), rate, dep, type);

I think you will need to post the rest of your program so we can see class declaration and other code. What you posted appears to be ok so the problem is probably elsewhere.
Last edited by Ancient Dragon; May 13th, 2007 at 7:27 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  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ Reading from a text file

 
0
  #10
May 14th, 2007
Also, "are not stored correctly" does not give enough information. Explain what actually does happen, because the result of the error may be important to understanding the problem.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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