| | |
Reading a file into a Parallel Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { int classid[4], test1[3], test2[3], test3[3], final[3], i, ; char name[9][14]; for(i=0; i<=8; i++)name[i][13]='\0'; //Open file... ifstream classrec; classrec.open("STUDNTRECS.TXT"); ofstream output; output.open("STUDENTRECSOUT.TXT"); //Read file and calculate the letter grade of each student... for(i=0; i<8; i++) { classrec.get(name[i],14); classrec>>classid[i]>>test1[i]>>test2[i]>>test3[i]>>final[i]>>ws; output<<name[i]<<" "<<classid[i]<<" "<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<final[i]<<endl; }
Last edited by Ancient Dragon; Sep 30th, 2007 at 11:27 pm. Reason: add code tags
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.
The size of classid is one character too small -- you have to make room for the null terminating byte.
C++ Syntax (Toggle Plain Text)
int count = 0; int c; while( !isdigit((c = cin.get()) ) { name[i][count] = c; ++count; } 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.
![]() |
Similar Threads
- Reading file input into an array (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Reading from a file to fill an array (Java)
- Error Message Concerning Reading File From A Drive (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Getting last value twice from file
- Next Thread: How to pass data to input?
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop 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 lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy 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 visual visualstudio win32 windows winsock wordfrequency wxwidgets






