c++ reading a a txt file line by line into a char array within an array of structures

user3427104 picture user3427104 · Mar 17, 2014 · Viewed 8k times · Source

I'm new to C++ and having a bit of trouble understanding the whole reading a file stream thing.. any help would be appreciated... here's where i'm having trouble

I Have an array of structures like this; (no I'm not allowed to use string to store these things apparently, or vectors or any other more advanced thing I haven't covered)...

struct Staff
{
    char title[TITLESIZE];
    char firstName[NAMESIZE];
    char familyName[NAMESIZE];
    char position[POSSIZE];
    char room[TITLESIZE];
    char email[POSSIZE];
};

Then I have an array of these structure;

Staff record[MAXNOSTAFF];

The data is contained in a text file separated by tabs. However some fields may contain whitespace. Data Like below:

Dr Sherine ANTOUN Lecturer 4327 3.204 [email protected]

Here is what I have written in my code...

//function prototypes
bool getRecord (ifstream& infile, Staff dataAr[], bool& fileFound);

int main()
{

    Staff record[MAXNOSTAFF];
    bool fileFound;
    ifstream infile;

    getRecord(infile, record, fileFound); //function call
    if (fileFound==true) 
    {
        cerr <<"Exiting Program"<<endl;
        exit(1);
    }

    return 0;
}

//function definitions
bool getRecord (ifstream& infile, Staff dataAr[], bool& fileFound)
{
    infile.open("phonebook.txt");

    if (infile)
    {
        fileFound = true;
        cout << "File " <<PHONEBOOK<< " opened successfully.\n\n";
    }
    else if (!infile)
    {
        fileFound =  false;
        cerr << "Error! File could not be opened. \n";
    }

    while (infile.good())
    {        

        for (int lineIndex=0; lineIndex<MAXNOSTAFF; lineIndex++)
            for (int titleIndex=0; titleIndex<TITLESIZE; titleIndex++)
            {
                cin.getline(dataAr[lineIndex].title[titleIndex], MAXNOSTAFF, '/t');
            }

    }
    //check it works properly
    for (int k=0;k<10; k++)
    {
        for (int m=0; m<11; m++)
        {
            cout << k <<". Title is : "<<dataAr[k].title[m]<<endl;
        }
    }    
    infile.close();
    return fileFound;
}

Any help would be greatly appreciated.. thank you

Answer

sehe picture sehe · Mar 18, 2014

Let me show you the Boost Spirit approach to parsing input data like this.

If you start with a struct like

struct Staff
{
    std::string title;
    std::string firstName;
    std::string familyName;
    std::string position;
    std::string room;
    std::string email;
};

You can use a Spirit grammar like:

    column = lexeme [ *~char_("\t\r\n") ];
    start  = column >> '\t'  >> column >> '\t' >> column >> '\t' >> column >> '\t' >> column >> '\t' >> column;

And parse all rows into a vector like:

    It f(std::cin), l;
    std::vector<Staff> staff_members;
    bool ok = qi::parse(f, l, grammar % qi::eol, staff_members);

    if (ok)
    {
        for(auto const& member : staff_members)
        {
            std::cout << boost::fusion::as_vector(member) << "\n";
        }
    } else
    {
        std::cout << "Parsing failed\n";
    }

    if (f != l)
        std::cout << "Remaining input '" << std::string(f, l) << "'\n";

Here's the complete test program Live on Coliru, sample run:

clang++ -std=c++11 -Os -Wall -pedantic main.cpp && ./a.out <<INPUT
Dr  Sherine ANTOUN  Lecturer    4327    3.204   [email protected]
Mr  Jason SCRYPT    Enthusiast  3472    9.204   [email protected]
INPUT

Output:

(Dr Sherine ANTOUN Lecturer 4327 3.204 [email protected])
(Mr Jason SCRYPT Enthusiast 3472 9.204 [email protected])
Remaining input '
'

Full Listing

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/tuple/tuple_io.hpp>

namespace qi = boost::spirit::qi;

struct Staff
{
    std::string title;
    std::string firstName;
    std::string familyName;
    std::string position;
    std::string room;
    std::string email;
};

BOOST_FUSION_ADAPT_STRUCT(Staff, 
    (std::string, title)
    (std::string, firstName)
    (std::string, familyName)
    (std::string, position)
    (std::string, room)
    (std::string, email))

template <typename It, typename Skipper = qi::unused_type>
    struct grammar : qi::grammar<It, Staff(), Skipper>
{
    grammar() : grammar::base_type(start)
    {
        using namespace qi;
        column = lexeme [ *~char_("\t\r\n") ];
        start  = column >> '\t'  >> column >> '\t' >> column >> '\t' >> column >> '\t' >> column >> '\t' >> column;
    }
    private:
    qi::rule<It, std::string(), Skipper> column;
    qi::rule<It, Staff(), Skipper> start;
};

int main()
{
    std::cin.unsetf(std::ios::skipws);

    typedef boost::spirit::istream_iterator It;
    grammar<It> grammar;

    It f(std::cin), l;
    std::vector<Staff> staff_members;
    bool ok = qi::parse(f, l, grammar % qi::eol, staff_members);

    if (ok)
    {
        for(auto const& member : staff_members)
        {
            std::cout << boost::fusion::as_vector(member) << "\n";
        }
    } else
    {
        std::cout << "Parsing failed\n";
    }

    if (f != l)
        std::cout << "Remaining input '" << std::string(f, l) << "'\n";
}