struct - how to access information in a file? C++ -


im having trouble 2 things, first in if statement choice c should take selected record file , change contents, not work reason file contents not change. in choice d need able read , add quantities , sales costs file , add them display them. im not sure start one. know need access info file, lines said data, add them , save them in variable displayed, how can access lines in file have data?

 #include <iostream>     #include <fstream>     #include <string>     #include "stdafx.h"     using namespace std;      struct info     {         // create inventory items info         string itemdescription;         int quantity;         double wholesalecost;         double retailcost;         string date;     };      int main()     {         //make instance of info , variable user selection         info item;         char choice;         // set functions         long bytenum(int);         void showrec(info);         void changerec(info);         //open file         fstream inventory("inventory.dat", ios::out | ios::in | ios::binary);         // loop user selection                 {             cout << "press 'a' add files\n";             cout << "press 'd' display files\n";             cout << "press 'c' change files\n";             cout << "press 'g' generate record\n";             cout << "or press 'q' quit\n";             cin >> choice;             //if add files record info put struct , save tofile             if (choice == 'a' || choice == 'a')             {                  cout << "enter item description\n";                 cin >> item.itemdescription;                 cout << "enter quantity on hand\n";                 cin >> item.quantity;                 cout << "enter whole sale cost\n";                 cin >> item.wholesalecost;                 cout << "enter item retail cost\n";                 cin >> item.retailcost;                 cout << "enter date added inventory\n";                 cin >> item.date;                 cout << "data added\n";                 inventory.write(reinterpret_cast<char *>(&item), sizeof(item));              }             //display record             else if (choice == 'd' || choice == 'd')             {                 int recordchoice;                 // record user wants                 cout << "enter record number want?";                 cin >> recordchoice;                 // display record info                 cout << " here record " << recordchoice << endl;                 inventory.seekg(bytenum(recordchoice), ios::beg);                 inventory.read(reinterpret_cast<char *>(&item), sizeof(item));                 showrec(item);              }             // change record info             else if (choice == 'c' || choice == 'c')             {                 int recordchoice;                 //get record user wants change                 cout << "enter record number want change?";                 cin >> recordchoice;                 // change struct info , save on old record in file                 changerec(item);                 inventory.seekp(bytenum(recordchoice), ios::beg);                 inventory.write(reinterpret_cast<char *>(&item), sizeof(item));              }             else if (choice == 'g' || choice == 'g')             {                   cout << "d";             }            } while (choice != 'q' || choice != 'q');           inventory.close();         return 0;     }      long bytenum(int recnum)     {         // record selection number         return sizeof(info) * recnum;     }      void showrec(info record)     {         // display record info         cout << record.itemdescription << endl;         cout << record.quantity << endl;         cout << record.wholesalecost << endl;         cout << record.retailcost << endl;         cout << record.date << endl;     }      void changerec(info record)     {         // change record info in struct         cout << "enter new item description\n";         cin >> record.itemdescription;         cout << "enter new quantity on hand\n";         cin >> record.quantity;         cout << "enter new whole sale cost\n";         cin >> record.wholesalecost;         cout << "enter new item retail cost\n";         cin >> record.retailcost;         cout << "enter new date added inventory\n";         cin >> record.date;         cout << "data added\n";     } 

your class consists of bunch of std::strings (and few other things, that's not important).

a std::string class implements text string-like interface. not string itself. typically, std::string consists of pointer heap-allocated buffer, size of string, , perhaps other metadata.

you don't have worry of that. have access std::string methods, substr(), , others, , result. class manages internal metadata, , responsible it. call substr(), class uses internal pointer find requested parts of string, , returns it, , on.

inventory.write(reinterpret_cast<char *>(&item), sizeof(item)); 

so, end writing binary contents of item structure file. includes bunch of std::strings. that's great. file contains has binary data includes raw memory addresses of bunch of heap-allocated buffers contain text strings. not contain text strings themselves.

when read them later, you'll same raw memory addresses. great, except that, now, it's different process, , raw memory addresses used process terminated time ago ...not useful. , when try access them, code blows up, because pointers meaningless nonsense.

ask following simple question: understand wrote sizeof(item) bytes, in write() call, above, right? , know sizeof(item) constant expression (or, @ least, 1 of first things instructor in c++ class should've told sizeof constant expression). so, question need ask how possible write constant number of bytes, whether or not 1 of strings in item structure contains ten characters, or ten thousand characters?

unfortunately, not simple write() , read() calls. need come file format represents contents of item class, , implement distinct methods write contents of fields, 1 one, file, , read back.

the comments question gave google food. use it.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -