Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

Basic C++ program, getline()/parsing a file

I've been tasked with creating a small program that is to parse through a text file and grab necessary info from it. The file is laid out as such

Tuesday*Info5051*10:00*11:00*M3039*Info5064*12:00*3:00*G1001;

Basically it's supposed to store each string in a struct so that I can later retrieve it, but I'm unable to get my program to work (I have a learning disability so things tend to get difficult). Here's my code so far. (I know it's a simple program but I tend to overthink/screw up stuff.) My big problem I've hit so far is that it won't open the file to start. I've saved the file to the bin->debug as well as the main folder of the program. I'm sure I'm using the getline method wrong.

struct Course
{
    string _sDay;
    string _sName;
    string _sCode;
    string _iStart;
    string _iDuration;
    string _sRoom;
};

int main()
{
    ifstream fileIn;
    fileIn.open("courseLoad.txt");

    vector<Course> vCourse;
    string str="*";
    string line;

    if (!fileIn)
    {
        cout<<"A error has occured, please contact support.";
    }

    while(!fileIn.eof())
    {
        for(int i=0; i!= fileIn.eof();i++)
        {
            //file.getline(entry.part_num, 6, '-');
            getline(fileIn,line,'*');
            vCourse[i]._sDay =line;
            getline(fileIn,line,'*');
            vCourse[i]._sName =line;
            getline(fileIn,line,'*');
            vCourse[i]._sCode = line;
            getline(fileIn,line,'*');
            vCourse[i]._iStart =line;
            getline(fileIn,line,'*');
            vCourse[i]._iDuration = line;
            getline(fileIn,line,'*');
            vCourse[i]._sRoom =line;

            cout<<vCourse[i];
        }//end for
    }
--output to screen here--
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several issue with this code:

1) That code is missing a return statement or an else statement to prevent the program from continuing its execution in case it cannot open the file:

if (!fileIn)
{
    cout<<"A error has occured, please contact support.";
    return -1;
}

2) Your getline all operate on the same input stream. You want to read in a line, then parse that line. For example:

// Read in a line
while (getline(fileIn,line))
{
    string item;
    std::stringstream sstr(line);

    // Read in an item
    while (getline(sstr, item, "*"))
    {
        std::cout << item << std::endl;
    }
}

3) vCourse size is 0, so you cannot use the [] operator; but you can use push_back to expand the size of the vector and insert an element at the back of the vector:

// Read in a line
while (getline(fileIn,line))
{
    string item;

    // Default course construction
    Course c;

    std::stringstream sstr(line);

    // Read in an item
    getline(sstr,item,'*');
    c._sDay = item;
    getline(sstr,item,'*');
    c._sName = item;
    getline(sstr,item,'*');
    c._sCode = item;
    getline(sstr,item,'*');
    c._iStart = item;
    getline(sstr,item,'*');
    c._iDuration = item;
    getline(sstr,item,'*');
    c._sRoom = item;

    // Save the course into the vector
    vCourse.push_back(c);
}

You could also add some more error checking in the above (in case some elements are missing from the line).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...