Reading from a file help !!

Hi guys ...
I have problem reading from a file
the file is an inventory file (text file) and it contains different data type :

make model partNo Quantity price partName
Pajero NA1H25 1 26 3.65 BLADE W/S WIPER
Pajero NA1S25 2 12 65.7 OIL SEAL-T/M CASE
Pajero NA3H25 3 20 14.6 OIL SEAL-DIST
Pajero NA3H25 4 26 10.95 DISC-CLUTCH
Pajero NC3V25 5 13 14.6 FUSIBLE LINK
Pajero ND0000 6 12 3.65 WEATHERSHIELD PKGE-
Pajero ND1V45 7 10 32.85 SEAL & BOOT KIT

I want to read these data and store them in arrays to edit and update them as i am doing an inventory control system program ...
what I did is this (to test it for the first 2 data {make and model} :
int main () {
string make;
string model;
int partNo;
int Quantity;
double price;
string partName ;
ifstream infile;
infile.open("inventory.txt");
while (!infile.eof()){
infile >> make >> model ;
cout << make << endl << model;
}
return 0;
}


But the outputs are not correct at all

Could you please help with this and tell me what i should do to read all the data in file and store them in arrays ???
THX
Last edited on
You are only reading the make and the model. So the next time you read the make, you are reading the partNo. You need to read ALL the different pieced of data before you can read the make again.
In this situation it may be clearer to read the file one line at a time, and then read each line on its own:
1
2
3
4
5
6
7
8
9
10
11
12

std::string line;

while(std::getline(infile, line))
{
    std::istringstream iss(line); // convert each line into an input stream

    if(iss >> make >> model)
    {
        std::cout << make << std::endl << model << std::endl;
    }
}


See std::getline()
http://cplusplus.com/reference/string/getline/

And std::istringstream
http://cplusplus.com/reference/iostream/istringstream/
I tried to do that :
int main () {
string make;
string model;
int partNo;
int Quantity;
double price;
string partName ;
ifstream infile;
infile.open("inventory.txt");
while (!infile.eof()){
infile >> make >> model >> partNo >> Quantity >> price >> partName ;
cout << make << model << partNo << Quantity << price << partName << endl;
}

An infinite loop occurred .... I need a way to read these data from the file and save them into an array , but first i need to make sure that the reading is OK by showing what i read from the file into the screen !!

by the way the partName field contains irregular pattern of white spaces.

Galik thank u for your help
It worked , however how could I save the data in an array to let the user edit,add,update or remove one of them ??
I am very grateful for your help ... I really appreciate it
Because the partName is the last field you can read it in, including all the whitespaces, using std::getline() after you have read all the other columns:
1
2
3
4
5
6
7
8
9
10
11
12
13
    while(infile) // keep looping if the last read was a success
    {
        // Read these collumns using >>
        infile >> make >> model >> partNo >> Quantity >> price;

        // Use std::getline() here to read the rest of the line (including spaces)
        std::getline(infile, partName);

        if(infile) // did the file reading succeed?
        {
            // add the item to the array (or a vector is better)
        }
    }


Rather than an array, I would consider using a std::vector. It would also make sense to create a struct for your records:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <string>

struct item
{
    std::string make;
    std::string model;
    int partNo;
    int Quantity;
    double price;
    std::string partName ;
};

std::vector<item> items;


See structs
http://cplusplus.com/doc/tutorial/structures/

See std::vector
http://cplusplus.com/reference/stl/vector/

Also using a vector
http://cplusplus.com/reference/stl/vector/push_back/
I did that :
int main () {
string make;
string model;
int partNo;
int Quantity;
double price;
string partName;
ifstream infile;
infile.open("inventory.txt");
data line;
while(!infile.eof()) // keep looping if the last read was a success
{
// Read these collumns using >>
infile >> make >> model >> partNo >> Quantity >> price;

// Use std::getline() here to read the rest of the line (including spaces)
getline(infile, partName);

data line = { make , model , partNo , Quantity , price , partName} ;// add the item to the array (or a vector is better)

}

It worked thanks to u galik

I'am making functions to add , edit , update , remove an item (-1) but i don't know if that possible with what i've done ..... any suggestion or any way to do that ???
Topic archived. No new replies allowed.