Problems loading a text file into a struct of vectors

This is a discussion on Problems loading a text file into a struct of vectors within the C++ Programming forums, part of the General Programming Boards category; I'm trying to open a text file and load its contents into a struct of vectors. The text file looks ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Problems loading a text file into a struct of vectors

    I'm trying to open a text file and load its contents into a struct of vectors. The text file looks something like this...

    638912 gibson@blah.blah 555-555-3827 Gibson, Martha Elizabeth
    582841 williams@blah.blag 555-555-4591 Williams, Henry James III
    719472 anderson@blah.bluh 555-555-6183 Anderson, Tina Marie

    The first number is stored in a vector of doubles, and the other 4 (email, phone num, first name, and last name) will be in string vectors.
    Each vector is a member of a struct.

    The program compiles fine but gives a segmentation fault error when I try to run it. My code for main() looks like this...
    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include "proj08.database.h"
    
    using namespace std;
    
    int main(int Argc, char *Arg[]){
            int i = 0;
            Student stuInfo;
            ifstream inFile;
            ofstream outFile;
    
            if(Argc != 2){
                    cout << "Improper amount of arguments." << endl;
            }
    
            inFile.open("project08.sample.students.old");
    
            while(!inFile.eof()){
                    inFile >> stuInfo.studentNum[i] >> stuInfo.email[i] >> stuInfo.phoneNum[i] >>
                              stuInfo.studentLastName[i] >> stuInfo.studentFirstName[i];
                    i++;
            }
            return 0;
    }
    The code in my header where the struct is contained looks like this...
    Code:
    #ifndef DBASE_
    #define DBASE_
    
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
     struct Student{
         vector<double> studentNum;
         vector<string> studentFirstName;
         vector<string> studentLastName;
         vector<string> email;
         vector<string> phoneNum;
     };
    
    #endif
    I am unsure of what exactly I am doing wrong here and any help would be appreciated. Thanks!
    Last edited by alt234; 12-04-2005 at 11:30 AM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
                    inFile >> stuInfo.studentNum[i] >> stuInfo.email[i] >> stuInfo.phoneNum[i] >>
    The vector studentNum is empty when you try to read into the string. -> crash.
    Read into temporary variables and use push_back() to insert into the vector.
    BTW : are you shure that your declaration of a Student is right. I can imagine that a Student has more then one e-mail or phone-number but usually they have only one last name.
    Kurt

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    inFile.open("project08.sample.students.old");
    Is this how you name your files?

    The only thing I'm wondering is what is arg[1] supposed to represent. I don't see your use of it anywhere in the program.

    ...and as ZuK was implying, it seems more logical to me to use an array of the stuct, not arrays in the struct.
    Last edited by SlyMaelstrom; 12-04-2005 at 11:46 AM.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Ok, thanks i'll check that out.

    The way I was doing this, the struct called stuInfo contains info on every student in the text file. So, vector email would contain the e-mail addresses for every student listed in the file and the vector lastName would contain the last names for every individual student in the text file. It seemed to me to be the only option otherwise I would have to have a seperate struct for every student listed in the text file and the amount of students listed within the file will not necessarily always be the same.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Oh, an array of structs I see. That does seem better. Sorry, I didn't understand thats what you meant first off Zuk.

    That file name is goofy, I know. It's just the sample one provided by the teacher and thats what he named it.

    EDIT:
    Forgot to answer the rest of your question. Arg1 will eventually represent the prefix for the file name. Eventually, the program will read 2 text files. Filename.students.old and Filename.transactions.old. The arg1 will be the Filename portion. The program will then know to open those files starting with that prefix and will then do some stuff and output to Filename.students.new and Filename.students.log.
    Last edited by alt234; 12-04-2005 at 11:52 AM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
     struct Student{
         double studentNum;
         string  studentFirstName;
         string  studentLastName;
         string  email;
         string  phoneNum;
     };
    
    struct Students {
       vector<Student> theStudents;
    };
    Would look more logical to me
    Kurt

    Edit: I see you got it.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Ok, im trying to implement this and am having some trouble again. I looked into push_back and I believe I have some type of a solution but i'm having all sorts of trouble.

    My includes are...
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <algorithm>
    #include "proj08.database.h"
    My code for reading the stuff in now mostly looks like this.
    Code:
    Student singleStuInfo;
    vector<Student> everyStudent;
    string line;
    ifstream inFile;
    
    inFile.open("project08.sample.students.old");
    
            if(!inFile.is_open()){
                    cout << "The file could not be opened." << endl;
            }
            else{
                    while(!inFile.eof()){
                            getline(inFile, line);
                            istringstream lineStream(line);
                            while(!lineStream.eof()){
                                    lineStream >> singleStuInfo.studentNum >> singleStuInfo.email
                                               >> singleStuInfo.phoneNum >> singleStuInfo.lastName
                                               >> singleStuInfo.firstName;
                            }
                            //everyStudent.push_back(singleStuInfo);
                    }
            }
    
            //cout << everyStudent.at(0) << endl;
            return 0;
    My struct is like this now...
    Code:
    struct Student{
         double studentNum;
         string firstName;
         string lastName;
         string email;
         string phoneNum;
    };
    Currently it is just entering an infinite loop and I am unsure why.
    Last edited by alt234; 12-04-2005 at 08:05 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You should use getline to read in the names. Currently you use operator>> which reads in a single word. However, the first names in your example are all multiple words. This leaves the rest of the line in the istringstream, which eventually fails because it reads a string instead of a double. Checking for eof() in the loop doesn't handle a failed stream, so it keeps trying to read and failing.

    The solution is to somehow use getline for the name to get the rest of the line after the other information is retrieved.

    Also, you should not be using eof() to control your while loop. One example of how that can cause problem is your current infinite loop. In both places, just put the first stream read into the loop control since it will evaluate to false if there is any problem including end of file:

    while(getline(inFile, line))
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Loading text from file into struct
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 05:33 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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