read contents of a text file into a struct

This is a discussion on read contents of a text file into a struct within the C++ Programming forums, part of the General Programming Boards category; reading the contents a file seems pretty straightforword, i.e.: Code: #include <iostream> #include <fstream> #include <string> using namespace std; int ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    read contents of a text file into a struct

    reading the contents a file seems pretty straightforword, i.e.:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    However, what if you wanted to place the contents of the file into a struct for further manipulation. For example if I know the file has a list of pre-sorted employee names and id numbers how would you read that data into the structure. So...it would be taking the raw data and reading into a struct something like:

    Code:
    struct EMPLOYEE {
           char firstname[40];
           char lastname[40];
           char employee_number[10];
    };
    I was thinking of first counting the total number of lines in the file and then dividing by 3 to determine the total number of "employees" that are in the file. Then from there going down the list and plugging in each line to a structure that identifies an employee.

    Does this sound like a valid approach? Also, any tips or links ot helpful tutorials on an effective way to go about assigning the data groups to their individual struct / reading from a text file into a structure?

    Also, how can I tell when there is a new line when reading a file? Do you just look the ASCII value for a newline?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It depends entirely on how your data file is designed. But essentially inside that big loop you've got, instead of just writing the line back out, you put it in whichever variable(s) are appropriate. (For instance, if you have the name on one line and the id on the next, you might call getline twice; the first time around you'll have to split up the firstname and lastname parts, the second time you can just put that in employee_number.) Alternatively and being smart you would just use >> to read in and be done with it.

    As far as "how can I tell when there is a new line when reading a file?" you don't. If you had paid any attention to the name "getline", or read the manual or a book, you might have realized that it will read until it finds a newline character.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    Also, you would be way better off to declare your struct as:

    Code:
    struct EMPLOYEE {
           std::string firstname;
           std::string lastname;
           std::string employee_number;
    };
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21