| | |
C++ Reading from a text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
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:
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:
c Syntax (Toggle Plain Text)
numEmp = 0; empFile >> id; empFile.ignore(); // Discard space before name empFile.getline(name, 21, '\n'); // Store up to 20 characters empFile >> rate >> dep >> type; //I think this is the problem while (empFile && numEmp < MAX_EMP) { // Move new employee info into array and increment numEmp count Emp[numEmp].set(id, name, rate, dep, type); numEmp++; // Get next employee info empFile >> id; outputFile << id << endl; empFile.ignore(); // Discard space before name empFile.getline(name, 21, '\n'); // Store up to 20 characters empFile >> rate >> dep >> type; }
Last edited by Ancient Dragon; May 13th, 2007 at 1:00 am. Reason: changed code tags to add line numbers
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.
line 5 should work ok if you make the above changes
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.
C++ Syntax (Toggle Plain Text)
string firstName,lastName; empFile>> firstName >> lastName; // now concantinate them together 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.
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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
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
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
I got around the issue of copying the string into an array using the following code:
C++ Syntax (Toggle Plain Text)
// tempName is a string, name is an array of characters tempName.copy(name, 21, 0);
Last edited by rockthrower; May 13th, 2007 at 5:40 pm. Reason: Formatting
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
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:
C++ Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>const int MAX_EMP = 100;</li> <li>int numEmp = 0;</li> <li>empFile >> id;</li> <li>empFile >> firstName >> lastName; // Extract first and last name</li> <li>tempName = firstName + " " + lastName; // Concantinate both names</li> <li>tempName.copy(name, 21, 0);</li> <li>empFile >> rate >> dep >> type; // Extract rest of varbiables</li> <li>while (empFile && numEmp < MAX_EMP)</li> <li>{</li> <li>// Move new employee info into array and increment numEmp count</li> <li>Emp[numEmp].set(id, name, rate, dep, type);</li> <li>numEmp++;</li> <li>// Get next employee info</li> <li>empFile >> id;</li> <li>empFile >> firstName >> lastName; // Extract first and last name</li> <li>tempName = firstName + " " + lastName; // Concantinate both names</li> <li>tempName.copy(name, 21, 0);</li> <li>empFile >> rate >> dep >> type; // Extract rest of varbiables</li> <li>}</li> </ol>
Last edited by rockthrower; May 13th, 2007 at 6:16 pm. Reason: Formatting
Rockthrower, don't manually use [list] bbcode to create line numbers. Instead use [code=c] or [code=cplusplus] bbcode.
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
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:
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:
c Syntax (Toggle Plain Text)
numEmp = 0; empFile >> id; empFile >> firstName >> lastName; // Extract first and last name tempName = firstName + " " + lastName; // Concantinate both names tempName.copy(name, 21, 0); // Copy string into array called name empFile >> rate >> type >> sex; // Extract the rest of the variables in line while (empFile && numEmp < MAX_EMP) { // Move new employee info into array and increment numEmp count Emp[numEmp].set(id, name, rate, dep, type); numEmp++; // Get next employee info empFile >> id; empFile >> firstName >> lastName; // Extract first and last name tempName = firstName + " " + lastName; // Concantinate both names tempName.copy(name, 21, 0); // Copy string into array called name empFile >> rate >> type >> sex; // Extract the rest of the variables in line }
Last edited by rockthrower; May 13th, 2007 at 7:05 pm. Reason: formatting
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:
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.
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Reading numbers from a text file (Java)
- Need Help in Reading characters from a text file (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: help pls...
- Next Thread: average array
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets







