This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: string in structure and fread()


Hi,

If you want to write and read a raw structure to a file, using c++
objects is NOT recommended at all! At least not using fread/fwrite

Given that fields have fixed length, a good solution could be using a
raw char array, something like this:

  #define FIELD_LENGTH  101

  struct book {
 Â ÂÂchar name[FIELD_LENGTH];
 Â ÂÂchar author[FIELD_LENGTH];
 Â ÂÂchar publisher[FIELD_LENGTH];
 Â ÂÂchar translator[FIELD_LENGTH];
 Â ÂÂbool translation;
 Â ÂÂbool stock;
 ÂÂ};


  // Write

  book mybook;

  fwrite(&mybook, sizeof(book), 1, filePtr);


  // Read

  book mybook;

  fread(&mybook, sizeof(book), 1, filePtr);


It will work awesomely, i promise!

Saludos!
 Â Juan




On Sun, Dec 25, 2011 at 11:01 PM, Mohsen Pahlevanzadeh
<mohsen@pahlevanzadeh.org> wrote:
>
> Dear all,
> I have a program that it should write a struct in binary file.My struct
> is here:
> ///////////////////
> struct book{
> Â Âstring name;
> Â Â Â Âstring author;
> Â Â Â Âstring publisher;
> Â Â Â Âstring translator;
> Â Â Â Âbool translation;
> Â Â Â Âbool stock;
> };
> ////////////////////////
> Then i wrote the following function that write to file:
> ////////////////////////////
> void WriteFile::addNode(string name, string publisher, string author,
> bool stock,string translator , bool translation ){
> Â Â Â Âbook * tmp = new book;
> Â Â Â Âint line = 1;
> Â Â Â Âshort unsigned i;
> Â Â Â Âstring memory;
>
> Â Â Â Âshort unsigned size = 100 - strlen((const char *)name.data());
> Â Â Â Âmemory = '`';
> Â Â Â Âfor (i =0 ;i<size;i++)
> Â Â Â Â Â Â Â Âmemory += '`';
> Â Â Â Âtmp->name = name Â+ memory;
>
> Â Â Â Âsize = 100 - strlen((const char *)publisher.data());
> Â Â Â Âmemory = '`';
> Â Â Â Âfor (i =0 ;i<size;i++)
> Â Â Â Â Â Â Â Âmemory += '`';
> Â Â Â Âtmp->publisher = Âpublisher Â+ memory;
>
> Â Â Â Âsize = 100 - strlen((const char *)author.data());
> Â Â Â Âmemory = '`';
> Â Â Â Âfor (i =0 ;i<size;i++)
> Â Â Â Â Â Â Â Âmemory += '`';
> Â Â Â Âtmp->author = author Â+ memory;
>
>
> Â Â Â Âsize = 100 - strlen((const char *)translator.data());
> Â Â Â Âmemory = '`';
> Â Â Â Âfor (i =0 ;i<size;i++)
> Â Â Â Â Â Â Â Âmemory += '`';
> Â Â Â Âtmp->translator = translator + memory;
>
> Â Â Â Âtmp->stock = stock;
> Â Â Â Âtmp->translation = translation;
>
> Â Â Â Âfseek(bookFilePtr,0L,SEEK_END);
> Â Â Â Âfwrite(ptr,408,1,bookFilePtr);
>
>
> ///////////////////////////
> Note that i wanted to use fixed length of each field, So, for example
> if a field has 30 character, i fill 71 character "`" at end of field. 30
> +71= 101
> So 4*string*101+2*bool*4=408 = length of struct.
>
> Then i wrote the following function to Âread all of records:
>
> ///////////////////////////////////////////////
> void ReadFile::printList(){
> Â Â Â Âfseek(bookFilePtr,0L,SEEK_SET); // set to begin of file
>
>
> Â Â Â Âint counter = 1;
> Â Â Â Âlong int line = 1;
> Â Â Â Âint pageCounter = 1;
>
>
> Â Â Â Âwhile ( fread(bookPtrObj,408,1,bookFilePtr) == 1 ){
> Â Â Â Â Â Â Â Âstring output;
> Â Â Â Â Â Â Â Âmvprintw(++line, 27,"***Title*****************Value*********" );
> Â Â Â Â Â Â Â Âoutput = "Name: Â Â Â Â Â Â Â " + bookPtrObj->name;
> Â Â Â Â Â Â Â Âmvprintw(++line, 27, output.data());
>
> Â Â Â Â Â Â Â Âoutput = "Publisher: Â Â Â Â Â" + bookPtrObj->publisher;
> Â Â Â Â Â Â Â Âmvprintw(++line, 27,output.data());
>
> Â Â Â Â Â Â Â Âoutput = "Author: Â Â Â Â Â Â " + bookPtrObj->author;
> Â Â Â Â Â Â Â Âmvprintw(++line, 27,output.data());
>
> Â Â Â Â Â Â Â Âoutput = "Translator: Â Â Â Â " + bookPtrObj->translator;
> Â Â Â Â Â Â Â Âif (bookPtrObj->translation == true )
> Â Â Â Â Â Â Â Â Â Â Â Âmvprintw(++line, 27,output.data());
>
> Â Â Â Â Â Â Â Âif (bookPtrObj->stock != true )
> Â Â Â Â Â Â Â Â Â Â Â Âmvprintw(++line, 27,"Stock: Â Â Â Â Â Â ÂThe given book doesn't
> exist.");
> Â Â Â Â Â Â Â Âelse
> Â Â Â Â Â Â Â Â Â Â Â Âmvprintw(++line, 27,"Stock: Â Â Â Â Â Â ÂThe given book exist.");
>
> Â Â Â Â Â Â Â Âif ( pageCounter % 3 == 0){
> Â Â Â Â Â Â Â Â Â Â Â Âmvprintw(++line, 27,"Press any key to see next page...");
> Â Â Â Â Â Â Â Â Â Â Â Âgetch();
> Â Â Â Â Â Â Â Â Â Â Â Âclear();
> Â Â Â Â Â Â Â Â Â Â Â Âline = 1;
>
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â ÂpageCounter++;
> Â Â Â Â Â Â Â Ârefresh();
>
> Â Â Â Â Â Â Â Âfseek(bookFilePtr, counter * sizeof(struct book) ,SEEK_SET); // seek
> to next data
> Â Â Â Â Â Â Â Âcounter ++;
> Â Â Â Â}
> }
>
> ///////////////////////////////////////////////
>
> But i get the segmentation fault on read each field.
> Because i didn't specify length of each field in read.How i specify
> length of each field in reading?
>
> --mohsen
>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]