I've been working a project that reads data from a text file that is formatted like this:
8ItemName ItemID Price Quantity
2ItemName ItemID Price Quantity
10ItemName ItemID Price Quantity
...
....
......
NOTE: There is no spacing between the beginning numbers(Customer ID) and the item Name.
This will keep going for about 200 records. As of now all I want to do is read in those records and store it in a binary file, as well as inserting the ItemID(the only unique number) into my AVL tree.
Here is my code:
// read a file into memory
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
int length = 70; //length of one line or record
char * buffer;
string temp;
int n;
struct record
{
int custID;
int partQuant;
string partDesc;
string partNum;
double partPrice;
};
record *data;
cout << "Please enter a record line: ";
cin >> n;
ifstream is;
is.open ("sales.dat", ios::in | ios::binary );
if( is.fail() ) //check if file exists
{
cout << "Cannot find the file 'sales.dat'." << endl;
system("pause");
return 0;
}
while(!(is.eof()))
{
is.read (buffer,length);
//cout.write (buffer, length);
}
int index = 0;
while (index <= n)
{
istringstream input(buffer);
input.get(data->partDesc, 40);
input >> data->partNum >> data->partPrice >> data->partQuant;
input.ignore(128,'\n');
cout << data->partDesc <<"$"<< data->partNum <<"$"<< data->partPrice <<"$"<< data->partQuant <<"$"<< endl;
index++;
}
is.close();
delete[] buffer;
system("pause");
return 0;
}
This code compiles, but then terminates when the CMD window is opened. If anyone could be so kind as to point out the error or a better solution to my particular problem I would be very grateful.
Thanks.

New Topic/Question
Reply




MultiQuote




|