My program saves customer data to a .txt file in binary. Fields such as name and address are typed in as a C++ string, converted to a C style char array, and saved to disk (since the compiler will not let me save a c++string, only a char array). These same fields can also be loaded into a C style char array and converted back to a string (for the ease of manipulation). I am using stringstreams to do the conversions but I am getting no results.
The customer name comes up just fine but all the other string fields come up blank. (the zip code works fine, also)Code:// save customer profile to disk int saveprof() { ofstream fcust(c:\\cust_data.txt",ios::binary | ios::app); if (fcust.fail()) { cout<<"\nCannot open library file!\a"; } else { string temp; temp.assign(custinfo.cust_name, 0, 26); fcust.write(temp.c_str(),26); temp.assign(custinfo.cust_address, 0, 26); fcust.write(temp.c_str(),26); temp.assign(custinfo.cust_city, 0, 16); fcust.write(temp.c_str(),16); temp.assign(custinfo.cust_state, 0, 3); fcust.write(temp.c_str(),3); fcust.write(reinterpret_cast<char*>(&custinfo.cust_zip), sizeof(int)); temp.assign(custinfo.cust_contact, 0, 26); fcust.write(temp.c_str(),26); temp.assign(custinfo.cust_phone, 0, 13); fcust.write(temp.c_str(),13); cout<<"\nFile saved"; fcust.close(); } return 0; } // load customer profile from disk int findprof() { ifstream fcust("c:\\cust_data.txt", ios::binary); if (fcust.fail()) { cout<<"\nCannot open library file!\a"; } else { istringstream sstream(ctemp); getline(sstream, custinfo.cust_name); sstream.str(""); fcust.read(ctemp,26); sstream.str(ctemp); getline(sstream, custinfo.cust_address); sstream.str(""); fcust.read(ctemp,16); sstream.str(ctemp); getline(sstream, custinfo.cust_city); sstream.str(""); fcust.read(ctemp,3); sstream.str(ctemp); getline(sstream, custinfo.cust_state); sstream.str(""); fcust.read(reinterpret_cast<char*>(&custinfo.cust_zip), sizeof(int)); fcust.close(); } return 0; }
Using DEV-C++ Under Windows XP



LinkBack URL
About LinkBacks



CornedBee