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
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;
}
}
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.
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:
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 ???