0

Here is an example from one of my data :

9789671539842,Zedeck Siew / Sharon Chin,Creatures of Near Kingdom,Vinlin Press Sdn Bhd,2018,1,24.00,2,2.

When I debug, the first data which is the isbn code will not be shown properly, for the first line, it would show 0, while for the rest of the lines, it would show numbers that were not from my text file, how can I fix that?

int file_reader()
{
    DATA d;
    ifstream infile("books.txt", ios::in);
    if (infile.is_open())
    {
        while (!infile.eof())
        {
            for (int row = 1; row < 42; row++)
            {
                cout << row << ". ";
                char line[200];
                infile.getline(line, 200, '\n');
                char* column;
                do
                {
                    column = strtok(line, ",");
                    d.isbn_code = atoi(column);
                    column = strtok(NULL, ",");
                    strcpy(d.author,column);
                    column = strtok(NULL, ",");
                    strcpy(d.title, column);
                    column = strtok(NULL, ",");
                    strcpy(d.publisher, column);
                    column = strtok(NULL, ",");
                    d.year_published = atoi(column);
                    column = strtok(NULL, ",");
                    d.quantity = atoi(column);
                    column = strtok(NULL, ",");
                    d.price = atof(column);
                    column = strtok(NULL, ",");
                    d.rack = atoi(column);
                    column = strtok(NULL, ",");
                    d.level_no = atoi(column);

                    cout << d.isbn_code << "," << d.author << "," << d.title << "," << d.publisher << "," << d.year_published << "," 
                        << d.quantity << "," << fixed << setprecision(2) << d.price << "," << d.rack << "," << d.level_no << endl;

                } while (column=="\0");
            } 
        } infile.close();
    }
    else
        cout << "File is not open\n";
    return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60

0 Answers0