(The book is C++ without Fear)
I was able to get it to work one way, but was not able to get the authors solution. Actually he uses different commands then in the book examples, but I like his approach so I decided to try and solve his way. He uses functions in the control statements which I didn't in the first go around.
Here is my first try which works.
and my second try which compiles, run, but it doesn't seem to write anything to the file and I get garbage back.Code:#include <iostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; int get_int(int default_value); int main() { char filename[MAX_PATH + 1]; int n = 0; int choice = 0; char model[21]; char make[21]; char year[6]; int mileage = 0; int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int); cout << "Enter file name: "; cin.getline(filename, MAX_PATH); // Open file for binary read and write. fstream fbin(filename, ios::binary | ios::out | ios::in); if (!fbin) { cout << "Could not open file " << filename << endl; system("PAUSE"); return -1; } // Get record number to write to. while(1){ cout << "What would you like to do?" << endl; cout << "Enter 1 to write a record." << endl; cout << "Enter 2 to read a record." << endl; cout << "Enter 3 to quit" << endl; cout << "Enter here: "; choice = get_int(0); if (choice == 3) break; if(choice == 1){ cout << "Enter file record number: "; n = get_int(0); // Get data from end user. cout << "Enter model: "; cin.getline(model, sizeof(model)); cout << "Enter make: "; cin.getline(make, sizeof(make)); cout << "Enter year: "; cin.getline(year, sizeof(year)); cout << "Enter mileage: "; mileage = get_int(0); // Write data to the file. fbin.seekp(n * recsize); fbin.write(model, sizeof(model) - 1); fbin.write(make, sizeof(make) - 1); fbin.write(year, sizeof(year) - 1); fbin.write((char*)(&mileage), sizeof(int)); } if (choice == 2){ cout << "Enter file record number: "; n = get_int(0); fbin.seekp(n * recsize); // Read data from the file. fbin.read(model, sizeof(model) - 1); fbin.read(make, sizeof(make) - 1); fbin.read(year, sizeof(year) - 1); fbin.read((char*)(&mileage), sizeof(int)); // Display the data cout << "The model is: " << model << endl; cout << "The make is: " << make << endl; cout << "The year is: " << year << endl; cout << "The mileage is: " << mileage << endl; } if (choice != (1 || 2 || 3)) continue; } fbin.close(); system("PAUSE"); return 0; } #define COL_WIDTH 80 // 80 is typical column width. // Get integer function // Get an integer from keyboard; return default // value if user enters 0-length string. // int get_int(int default_value) { char s[COL_WIDTH + 1]; cin.getline(s, COL_WIDTH); if (strlen(s) == 0) return default_value; return atoi(s); }
any ideas? keep in mind I'm a beginner to C++, if you see any other errors I may be making please let me have it. Braces for impact.Code:#include <iostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; int get_int(int default_value); void write_record(); void read_record(); int main() { char filename[MAX_PATH + 1]; int n = 0; int choice = 0; cout << "Enter file name: "; cin.getline(filename, MAX_PATH); // Open file for binary read and write. fstream fbin(filename, ios::binary | ios::out | ios::in); if (!fbin) { cout << "Could not open file " << filename << endl; system("PAUSE"); return -1; } // Get record number to write to. while(1){ cout << "What would you like to do?" << endl; cout << "Enter 1 to write a record." << endl; cout << "Enter 2 to read a record." << endl; cout << "Enter 3 to quit" << endl; cout << "Enter here: "; choice = get_int(0); if (choice == 1) write_record(); else if(choice == 2) read_record(); else if(choice == 3){ fbin.close(); system("PAUSE"); return 0;} else cout << "Bad Entry!!! Re-enter choice: " << endl << endl; } //end while } // end main void write_record(){ int n = 0; int choice = 0; char model[21]; char make[21]; char year[6]; int mileage = 0; int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int); fstream fbin; cout << "Enter file record number: "; n = get_int(0); cout << "Enter model: "; cin.getline(model, sizeof(model)); cout << "Enter make: "; cin.getline(make, sizeof(make)); cout << "Enter year: "; cin.getline(year, sizeof(year)); cout << "Enter mileage: "; mileage = get_int(0); // Write data to the file. fbin.seekp(n * recsize); fbin.write(model, sizeof(model) - 1); fbin.write(make, sizeof(make) - 1); fbin.write(year, sizeof(year) - 1); fbin.write((char*)(&mileage), sizeof(int)); } void read_record(){ int n = 0; int choice = 0; char model[21]; char make[21]; char year[6]; int mileage = 0; int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int); fstream fbin; cout << "Enter file record number: "; n = get_int(0); fbin.seekp(n * recsize); // Read data from the file. fbin.read(model, sizeof(model) - 1); fbin.read(make, sizeof(make) - 1); fbin.read(year, sizeof(year) - 1); fbin.read((char*)(&mileage), sizeof(int)); // Display the data cout << "The model is: " << model << endl; cout << "The make is: " << make << endl; cout << "The year is: " << year << endl; cout << "The mileage is: " << mileage << endl; } #define COL_WIDTH 80 // 80 is typical column width. // Get integer function // Get an integer from keyboard; return default // value if user enters 0-length string. // int get_int(int default_value) { char s[COL_WIDTH + 1]; cin.getline(s, COL_WIDTH); if (strlen(s) == 0) return default_value; return atoi(s); }
Thanks all!



LinkBack URL
About LinkBacks


