You are here:

C++/textread function

Advertisement


Question
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:
 John|83|52.2
 swimming|Jefferson
 Jane|26|10.09
 sprinting|San Marin
Process using:
std::ifstream inf("data.txt");
char name[30];
while(!inf.getline(name, 30, '|').eof())
{
 Athlete* ap;
 char jersey_number[10];
 char best_time[10];
 char sport[40];
 char high_school[40];
 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...

struct product
{
      long int stockNo;
      char product[20];
      char supply[20];
      long int quantity;
      int dateArrived;
};
int numProd;
product pro[max], temppro[max], sortpro[max], sortpro1[max];

int i=numProd;
    numProd+=1;
    ofstream fout;
    fout.open("products.txt",ios::app);
         
    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) << "*";
    fout.close();


...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

Answer
> 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.
see: http://cplusplus.com/reference/clibrary/cstdlib/strtod/

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) << '*'
    // etc
    << '\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

see: http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
http://www.daniweb.com/software-development/cpp/tutorials/71858  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.