ok here is an example of the type txt file I am trying to read

12345678901234567890123456789012345678901234567890
Bugs Bunny Jr.      1234 1001.01
Dr. Wiley Coyote    2345 1002.02
Taco Speedy Gonzales3456 1003.03
Billy the Goat      4567 1004.04
Porky Pig           5678 1005.05

what I am trying to do is skip the first line and then read the rest storing it in a linked list of structs the problem I am running into is that it will get all the way to the third line then skip the fourth read the fifth and get junk for the sixth.

here is the read function I am using all variables are global

void readfilefn()
{
    firstcustomer=new customerType;
    fin.ignore(50,'\n');
    fin.get(firstcustomer->name, 21);
    currcustomer=firstcustomer;
    fin>>currcustomer->pin;
    fin>>currcustomer->balance;
    firstcustomer->link=currcustomer;
    //check if the file is being read properly
    cout<<"\nCustomer Name: "<<firstcustomer->name<<endl;
    cout<<"Pin          : "<<firstcustomer->pin<<endl;
    cout<<"Balance      : "<<firstcustomer->balance<<endl;

    counter++;

    while(!fin.eof()&&fin.peek()=='\n')
    {
        newcustomer=new customerType;
        int row=0;
        while(row<counter)
        {
            fin.ignore(50,'\n');
            row++;
        }
        fin.get(newcustomer->name, 21);
        fin>>newcustomer->pin;
        fin>>newcustomer->balance;
        currcustomer->link=newcustomer;
        currcustomer=newcustomer;
        cout<<"\nCustomer Name: "<<currcustomer->name<<endl;
        cout<<"Pin          : "<<currcustomer->pin<<endl;
        cout<<"Balance      : "<<currcustomer->balance<<endl;
        //if(fin.peek()!='\n')fin.ignore();
        counter++;
    }//end of while


    cout<<"There are "<<counter<<" accounts"<<endl;

}

Recommended Answers

All 4 Replies

Hi mate,

I didn't read through all the source code, but basically you can use getline to do this.

getline has the following parameters:
std::getline ( source stream , desination stream , delimeter )

There are several ways to use this..

e.g.

...
std::string temp, total;
while (!file.eof())
{
    std::getline(file,temp); // delimeter is the newline char by default
    total += temp;
}

or something like this:

...
while(std::getline(file,temp))
    str += temp;

I shouldn't be giving away the solution so cheaply, but it will be something like this:

getline(file,str);

for (int counter = 0; !file.eof(); counter++)
{
    getline(file,someKindOfString);
    // do whatever else with string
}

cout << counter << "accounts";

Thank you for your response I figured it out, the initialization of the variable row should have been outside of the outter while loop.

...
while(std::getline(file,temp))
    str += temp;

Of the 3 solutions you posted, this is the only one which won't crash and burn once the file hits EOF. the EOF flag is not set until after the stream reaches EOF (A read is attempted and fails) - which means your final call to getline will break, causing problems for the final iteration of the loop.

The loop in this example doesn't rely on EOF, so it works well.

As a general rule of thumb, you should never use EOF as a loop's terminating condition when reading from a file.

The problem probably is at Speed Gonzales3456. There is no space between es and 3456, so afterwards everything is out of sync by one field.

You last post wasn't visible to me until I posted this one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.