Thread: Reading Multiple Pieces of Data From Text File

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Reading Multiple Pieces of Data From Text File

    Hi,

    I've written this simple program to get some data from a text file. It reads in data to a string, an int array, another int array, and a double array. The reason there are 2 int arrays is because one of them was originally going to be for unsigned ints only. I changed it back to int because I wasn't sure if it was causing a problem with the program (although I don't think it would now).

    Ultimately, the string and the arrays are just temporary, obviously. All of the data read in will be used to build an object of a player class after each of the variables has been filled.

    Anyway basically the reason I'm posting is because I get this bug every time I run the program. It says "the stack around the variable data1 is corrupted". Anyone know why?

    Also, are there many ways I could make this program better? I'm guessing there are ways to not use c strings as much.

    Thanks.

    The "players.txt" text file is the following:

    List of players in the game and their attributes.

    1. Arnold, 0, 0, 0, 0, -1, 1, 4, 5, 0, 0
    2. Barry, 1, 480, 240, 2, 5, 8, 1, 1, 8, 10.5
    3. Colin, 2, 480, 240, 2, 9, 1, 7, 8, 2, 10.5
    4. Dave, 3, 480, 240, 2, 3, 8, 5, 2, 9, 10
    5. Edmund, 4, 480, 240, 2, 9, 7, 8, 1, 9, 10
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream file_in("players.txt");
        cout << "Reading player data from players.txt" << endl << endl;
    
        char line_from_file[499];
        char data_from_str[7];
        string str_from_file;
    
        string name;
        int data1[3];
        int data2[6];
        double data3;
        int pos = 0;
        int j = 0;
    
        while (!file_in.eof())
        {
            for (pos = 0; pos < 498; pos++)
                line_from_file[pos] = ' ';
            file_in.getline(line_from_file, 499); // terminates the string with '\0'
            // Check if line_from_file begins with a number. If not, don't check for data.
            if (line_from_file[0] >= 48 && line_from_file[0] <= 57)
            {
                str_from_file.clear();
                name.clear();
                // If necessary, skip past the second digit of the number at the start of each line. 
                // Pos ends up being 3 (1 digit numbers/ 0-9) or 4 (2 digit numbers/ 10+).
                if (line_from_file[1] >= 48 && line_from_file[1] <= 57)
                    pos = 4;
                else
                    pos = 3;
                while (line_from_file[pos]) { // space=32, endofstring='\0'=0
                    str_from_file += line_from_file[pos];
                    pos++;
                }
                pos = 0;
                // remove all the white space from str_from_file
                while (str_from_file.find(' ') != string::npos) // gets rid of spaces
                    str_from_file.erase(str_from_file.find(' '), 1);
                while (str_from_file.find(9) != string::npos)   // gets rid of tabs
                    str_from_file.erase(str_from_file.find(9), 1);
                // Read upto the first comma- the name.
                while (str_from_file[pos] != ',')
                {
                    name += str_from_file[pos];
                    pos++;
                }
                cout << name << " ";
                // Read upto each of the following commas- the rest of the data.
                pos = 0;
                for (int i = 0; i < 10 && str_from_file[i]; i++)
                {
                    while (str_from_file[pos] != ',' && str_from_file[pos])
                        pos++;
                    pos++;
                    data_from_str[0] = '\0';
                    for (j = 0; j < 7 && str_from_file[pos+j] != ',' && str_from_file[pos+j]; j++)
                        data_from_str[j] = str_from_file[pos+j];
                    // if a non-number is found, terminate data_from_str with a null.
                    if (str_from_file[pos+j] < 48 || str_from_file[pos+j] > 57)
                        data_from_str[j] = '\0';
                    cout << data_from_str << " "; //output what was read into the char array.
                    if (i <= 2)
                        data1[i] = atoi(data_from_str);
                    else if (i <= 8)
                        data2[i] = atoi(data_from_str);
                    else if (i == 9)
                        data3 = atof(data_from_str);
                }
                cout << endl;
            }
        }
        file_in.close();
        file_in.clear();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are trying to access data2[3] through data2[8] instead of data2[0] through data2[5].

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Quote Originally Posted by tabstop View Post
    You are trying to access data2[3] through data2[8] instead of data2[0] through data2[5].
    lol. That was a pretty stupid error. Thanks.

    Any ideas on how I could improve the program generally? Is using getline/ c strings ok for this? Or should I be doing more with std::string?

    Thanks again.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while (!file_in.eof())

    FAQ explains why you should not use eof flag to control read loop - check the return value of the read function instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM