Thursday, May 5, 2011

input from txt file to arrays

I have a text file with a line like:

James Dean         10 Automotive      27010.43

and I need to read that file and put each of the 4 above into arrays.

char nameArray[MAX][NAME_MAX];
int yearArray[MAX];
char departmentArray[MAX][DEP_MAX];
double payArray[MAX];


while(i < MAX && infile) {

     infile.getline(nameArray[i], 20);
     infile >> yearArray[i];
     infile.getline(departmentArray[i], 15);
     infile >> payArray[i];

     cout << nameArray[i] << "  " << yearArray[i] << "  " << departmentArray[i] << "  " << fixed << setprecision(2) << payArray[i] << endl;
     i++;
    }

The code is cut down just to give you an idea of what I am trying to do, but when I run this, I get something like:

James Dean         -858993460    -92559631349317830000000000000000000000000000
000000000000000000.00

Thanks for the help.

==== Edit ==========================================

I changed from getline to get, thanks for that. I have to use get and not >> because some of the lines I am reading in are more than just "James Dean", they are up to 20 char long...ex: "William K. Woodward" is another one.

So, if I just use get, then it reads the first line in fine, but then I get the same messed up text for the second line.

Here is the code:

 infile.get(nameArray[i], 20);
 infile >> yearArray[i];
 infile.get(departmentArray[i], 15);
 infile >> payArray[i];
From stackoverflow
  • The getline functions takes an input stream and a string to write to. So, two getline calls read in two lines. Your input mechanism is broken. Either, use getline or the stream extraction operator (i.e. >>) but not both.

    If you plan to use getline you need to parse the string (which is effectively one line of input) into tokes, and then store them in appropriately typed arrays. The second and fourth tokens are numbers, hence you will need to convert these from string to int or double.

    The operator >> approach:

    string name, surname;
    int year;
    double pay;
    while (infile) {
        infile >> name >> surname >> year >> department >> pay;
        namearray[ i ] = name + " " + surname;
        // ...
        payarray[ i ] = pay;
        ++i;
    }
    

    The getline approach:

    string line;
    while (getline(infile, line)) {
        parse(line, tokens);
        namearray[ i ] = token[ 0 ] + " " + token[ 1 ];
        // ...
        payarray[ i ] = strTodouble(token[ 4 ]);
        ++i;   
    }
    
    // parse definition
    void parse(string line, vector<string>& token) {
        // roll your own
    }
    
    double strToDouble(string s) {
       // ...
    }
    
  • I dont see where you define infile but I will assume that it is an ifile . In that case you should use it the same way u use cin to get input.

    Why do you do a getline () ? That function will stop only at an '\n' char or at an EOF char. So it means, you start reading the int after the end of the line, some random data.

    Correct me if i'm wrong, but are there 20 or 19 characters in that first string (James Dean) before the number (10) ?

0 comments:

Post a Comment