Thread: Problem with reading a file into array of struct

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Problem with reading a file into array of struct

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    struct inventory
    {
        int barcode;
        char description[512];
        float price;
        int quantity;
    };
    
    
    int main()
    {
        cout << fixed;
        cout << setprecision(2);
    
        inventory s[4];
    
        char filename[128];
        ifstream infile;
        cout <<"Enter the file name : ";
        cin.getline(filename, 128);
        infile.open(filename);
    
            while (!infile)
            {
            cout << "File could not be opened"<<endl;
            cout <<"Enter the file name again :";
            cin.getline(filename, 128);
            infile.open(filename);
            }
    
    
    
            while (!infile.eof())
            {
                if (!infile.eof())
                {
                for(int i = 0; i < 4; i++)
                {
                infile >> s[i].barcode;
                cin.ignore();
                infile.getline(s[i].description, 512);
                infile >> s[i].price;
                infile >> s[i].quantity;
                }
                }
            }
    
    
    
            for(int i = 0; i < 4; i++)
            {
            cout << "barcode : " << s[i].barcode << endl;
            cout << "description : " << s[i].description << endl;
            cout << "price : " << s[i].price << endl;
            cout << "quantity : " << s[i].quantity << endl;
            cout << endl;
            }
    
    
    
            return 0; 
    }
    This is my text file
    20123451
    Sugar packet (1 kg)
    2.50
    560
    21347652
    Milo 3-in-1
    12.50
    200
    20123453
    Salt (1kg)
    4.50
    270
    21347654
    Nescafe 3-in-1
    12.50
    400
    20123455
    Tamatoes
    4.00
    100
    21347656
    Cucumbers
    4.00
    280



    This is my program output
    Enter the file name : "filename".txt
    ..... (Nothing shows up)



    Any help would be greatly appreciated.
    Last edited by Lozy; 11-21-2014 at 12:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that your loop might never end: EOF is detected when an attempted read fails, but you are only reading 4 times. Furthermore, you did not consider what happens if there are fewer than 4 entries. I suggest something like this:
    Code:
    int inventory_count = 0;
    while (inventory_count < 4)
    {
        if (infile >> s[inventory_count].barcode &&
            infile.ignore() &&
            infile.getline(s[inventory_count].description, 512) &&
            infile >> s[inventory_count].price >> s[inventory_count].quantity)
        {
            ++inventory_count;
        }
        else
        {
            break;
        }
    }
    
    for (int i = 0; i < inventory_count; ++i)
    {
        cout << "barcode : " << s[i].barcode << endl;
        cout << "description : " << s[i].description << endl;
        cout << "price : " << s[i].price << endl;
        cout << "quantity : " << s[i].quantity << endl;
        cout << endl;
    }
    This way, you test the return value of the reading functions instead of testing with eof(). You keep track of the number of entries read without assuming that there will be exactly 4 of them (but using the loop condition to limit them to a maximum of 4). Finally, you print based on the number of entries read, again without assuming that there are exactly 4 entries to print.

    I have taken the liberty of changing your cin.ignore() to infile.ignore().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    Hi laserlight, thank you very much ! for correcting my codes.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you use std::string instead of char arrays.
    Using the >> operator with char arrays is not safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-04-2013, 04:29 PM
  2. reading the content of a file into an array of struct
    By m_programmer in forum C Programming
    Replies: 6
    Last Post: 09-20-2012, 07:29 AM
  3. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Reading a txt file into a struct problem
    By Swerve in forum C++ Programming
    Replies: 10
    Last Post: 03-19-2008, 12:56 AM