Hi sir. I was really get dumped for more than a month completing this project of mine. Just as I thought I have finished it, this new problem arise. You know, I'm making an inventory system with file handling. I already made to append, read, delete the contents of the file but after i close and reopen the program, i cannot restore the data on the class arrays i've made. Anyway, I found this on the net and thought this would be the solution to my problem...
Delimited files can easily be read using a while loop and getline.
Given data file:
while(!inf.getline(name, 30, '|').eof())
inf.getline(jersey_number, 10, '|'); #read thru pipe
inf.getline(best_time, 10); #read thru newline
inf.getline(sport, 40, '|'); #read thru pipe
inf.getline(high_school, 40); #read thru newline
ap = new Athlete(name, strtod(number), strtof(best_time), sport, high_school);
//do something with ap
In a delimited file, only the first field should be in the while loop
For each field: If the field is the last field in the line or the only field in the line, be sure that getline stops at a newline and not some other delimiter
i cannot locate the errors of this codes, and there's a whole lot of new functions i do not recognize like startod(), startof()...
....my problem is very similar to this codes... suppose i have this contents on the file
word1 * word2 * word3 * word4
word5 * word6 * word7 * word8
...now, the way i appended this data on the file went here...
long int stockNo;
long int quantity;
product pro[max], temppro[max], sortpro[max], sortpro1[max];
cout << "Enter the following items: \n";
cout << "\t Stock No.\t\t: ";
cin >> pro[i].stockNo;
cout << "\t Product\t\t: ";
cin >> pro[i].product;
cout << "\t Quantity\t\t: ";
cin >> pro[i].quantity;
cout << "\t Supplier\t\t: ";
cin >> pro[i].supply;
cout << "\t Date Arrived (mmddyy)\t: ";
cin >> pro[i].dateArrived;
cout << endl<<endl;
fout << endl <<setw(1) << "*"
<<setw(8) <<pro[i].stockNo << setw(2) << "*"
<<setw(20)<<pro[i].product << setw(2) << "*"
<<setw(10)<<pro[i].quantity << setw(2) << "*"
<<setw(20)<<pro[i].supply << setw(2) << "*"
<<setw(13)<<pro[i].dateArrived << setw(2) << "*";
...when i want to reopen the file, i want to store back the data in their corresponding arrays in the class Product so that i could manipulate on them again as is..
looking forward for your respond.. thank you .. :D
> there's a whole lot of new functions i do not recognize like startod(), startof()...
These are C functions to convert strings to numeric types.
In C++, the preferred way do this is to use a std::stringsteam
> when i want to reopen the file, i want to store back the data in their corresponding arrays
> in the class Product so that i could manipulate on them again as is..
I would suggest that you put all the information for one product on one line, the next product on the next line and so on. And since you are using a delimiter '*', avoid the setw().
fout << pro[i].stockNo << setw(2) << '*'
<< pro[i].product << setw(2) << '*'
<< '\n' ; // finally put a new line
To read this back,
a. first read the entire line into a std::string using std::getline()
b. construct an input stringstream using the string just read
c. read off the product information from the std::istringstream