Hydlide
Hydlide

Reputation: 363

Reading from a text file and inserting data into an array

Most of the information I've found is based on numbers, however I want to use words. For instance, if my text file looks like this:

M
Gordon
Freeman
Engineer
F
Sally
Reynolds
Scientist

I want to be able to put each line into an array and output it like so:

Gender: M
First Name: Gordon
Last Name: Freeman
Job: Engineer
Gender: F
First Name: Sally
Last Name: Reynolds
Job: Scientist

This list could go on and on, but two is good for now.

I'm currently using a struct to hold the information:

struct PeopleInfo
{
    char gender; 
    char name_first [ CHAR_ARRAY_SIZE ];
    char name_last [ CHAR_ARRAY_SIZE ];
    char job [ CHAR_ARRAY_SIZE ];
};

I'm not sure if I need to use a delimiter or something to tell the program when to stop at each part (gender, first name, last name, etc). Could I use the getline function with ifstream? I'm having trouble implementing that in my own code. I'm not really sure where to start as I haven't had to use anything like this for a while now. Frantically searching through textbooks and Google to find similar problems, but so far I haven't had much luck. I will update my post with any questions and code that I discover.

Upvotes: 0

Views: 1776

Answers (5)

Jerry Coffin
Jerry Coffin

Reputation: 490768

I think @user1200129 is on the right track, but hasn't quite gotten all the pieces put together yet.

I'd change the structure just a little bit:

struct PeopleInfo
{
    char gender; 
    std::string name_first;
    std::string name_last;
    std::string job;
};

Then I'd overload operator>> for that structure:

std::istream &operator>>(std::istream &is, PeopleInfo &p) { 
    is >> p.gender;   
    std::getline(is, p.name_first);
    std::getline(is, p.name_last);
    std::getline(is, p.job);
    return is;
}

Since you want to be able to display them, I'd add an operator<< to do that too:

std::ostream &operator<<(std::ostream &os, PeopleInfo const &p) { 
    return os << "Gender: " << p.gender << "\n"
              << "First Name: " << p.name_first << "\n"
              << "Last Name: " << p.name_last << "\n"
              << "Job: " << p.job;
}

Then reading in a file full of data can be something like this:

std::ifstream input("my file name");

std::vector<PeopleInfo> people;

std::vector<PeopleInfo> p((std::istream_iterator<PeopleInfo>(input)),
                          std::istream_iterator<PeopleInfo(),
                          std::back_inserter(people));

Likewise, displaying the people's info from the vector goes something like:

std::copy(people.begin(), people.end(),
          std::ostream_iterator<PeopleInfo>(std::cout, "\n"));

Upvotes: 3

glu
glu

Reputation: 195

You can also use your std::ifstream file as any other stream:

//your headers
int main(int argc, char** argv)
{
    std::ifstream file("name.txt");
    std::string line;
    std::vector<std::string> v; // You may use array as well

    while ( file.eof() == false ) {
        file >> line;
        v.push_back( line );
    }

    //Rest of your code
    return 0;
}

Upvotes: 0

01100110
01100110

Reputation: 2354

A struct may be better than an array for storing the information.

struct person
{
    std::string gender;
    std::string first_name;
    std::string last_name;
    std::string position;
};

You could then have a vector of persons and iterate over that.

Upvotes: 1

Ambidextrous
Ambidextrous

Reputation: 820

You could also use a flag like bool maleFlag and bool femaleFlag, and set them to true and false as and when you read only 'M' or 'F' on a line, so you know what gender to associate to the names that follow.

Upvotes: 0

Jesse Good
Jesse Good

Reputation: 52405

Well to get you started:

// Include proper headers here
int main()
{
    std::ifstream file("nameoftextfilehere.txt");
    std::string line;
    std::vector<std::string> v; // Instead of plain array use a vector

    while (std::getline(file, line))
    {
        // Process each line here and add to vector
    }

    // Print out vector here
 }

Upvotes: 0

Related Questions