From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23262 invoked by alias); 26 Dec 2011 10:30:27 -0000 Received: (qmail 23254 invoked by uid 22791); 26 Dec 2011 10:30:26 -0000 X-SWARE-Spam-Status: No, hits=1.7 required=5.0 tests=AWL,BAYES_00,DATE_IN_FUTURE_03_06,SPF_NEUTRAL X-Spam-Check-By: sourceware.org Received: from smtp.hosting.parsonline.net (HELO smtp.hosting.parsonline.net) (213.217.60.12) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Mon, 26 Dec 2011 10:30:11 +0000 Received: (qmail 68897 invoked from network); 26 Dec 2011 10:51:52 -0000 Received: from 91.98.156.6.pol.ir (HELO ?192.168.2.101?) (mohsen@pahlevanzadeh.org@91.98.156.6) by 0 with SMTP; 26 Dec 2011 10:51:52 -0000 Message-ID: <1324907980.2464.2.camel@debian> Subject: Re: string in structure and fread() From: Mohsen Pahlevanzadeh To: Juan =?ISO-8859-1?Q?Ram=EDrez?= Cc: gcc-help Date: Mon, 26 Dec 2011 11:37:00 -0000 In-Reply-To: References: <1324861286.2539.24.camel@debian> Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-4KOWmC1iSKvWODcCMv8h" Mime-Version: 1.0 X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-12/txt/msg00235.txt.bz2 --=-4KOWmC1iSKvWODcCMv8h Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Content-length: 5300 Hi , I read email's Sam carefully, but i didn't test yet.Do you think if i implement same Sam i can implement string obj in structure? --mohsen On Sun, 2011-12-25 at 21:04 -0200, Juan Ram=C3=ADrez wrote: > Hi, >=20 > 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 >=20 > Given that fields have fixed length, a good solution could be using a > raw char array, something like this: >=20 > #define FIELD_LENGTH 101 >=20 > struct book { > char name[FIELD_LENGTH]; > char author[FIELD_LENGTH]; > char publisher[FIELD_LENGTH]; > char translator[FIELD_LENGTH]; > bool translation; > bool stock; > }; >=20 >=20 > // Write >=20 > book mybook; >=20 > fwrite(&mybook, sizeof(book), 1, filePtr); >=20 >=20 > // Read >=20 > book mybook; >=20 > fread(&mybook, sizeof(book), 1, filePtr); >=20 >=20 > It will work awesomely, i promise! >=20 > Saludos! > Juan >=20 >=20 >=20 >=20 > On Sun, Dec 25, 2011 at 11:01 PM, Mohsen Pahlevanzadeh > 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 =3D new book; > > int line =3D 1; > > short unsigned i; > > string memory; > > > > short unsigned size =3D 100 - strlen((const char *)name.data()); > > memory =3D '`'; > > for (i =3D0 ;i > memory +=3D '`'; > > tmp->name =3D name + memory; > > > > size =3D 100 - strlen((const char *)publisher.data()); > > memory =3D '`'; > > for (i =3D0 ;i > memory +=3D '`'; > > tmp->publisher =3D publisher + memory; > > > > size =3D 100 - strlen((const char *)author.data()); > > memory =3D '`'; > > for (i =3D0 ;i > memory +=3D '`'; > > tmp->author =3D author + memory; > > > > > > size =3D 100 - strlen((const char *)translator.data()); > > memory =3D '`'; > > for (i =3D0 ;i > memory +=3D '`'; > > tmp->translator =3D translator + memory; > > > > tmp->stock =3D stock; > > tmp->translation =3D 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=3D 101 > > So 4*string*101+2*bool*4=3D408 =3D 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 =3D 1; > > long int line =3D 1; > > int pageCounter =3D 1; > > > > > > while ( fread(bookPtrObj,408,1,bookFilePtr) =3D=3D 1 ){ > > string output; > > mvprintw(++line, 27,"***Title*****************Value*****= ****" ); > > output =3D "Name: " + bookPtrObj->name; > > mvprintw(++line, 27, output.data()); > > > > output =3D "Publisher: " + bookPtrObj->publishe= r; > > mvprintw(++line, 27,output.data()); > > > > output =3D "Author: " + bookPtrObj->author; > > mvprintw(++line, 27,output.data()); > > > > output =3D "Translator: " + bookPtrObj->translat= or; > > if (bookPtrObj->translation =3D=3D true ) > > mvprintw(++line, 27,output.data()); > > > > if (bookPtrObj->stock !=3D true ) > > mvprintw(++line, 27,"Stock: The giv= en book doesn't > > exist."); > > else > > mvprintw(++line, 27,"Stock: The giv= en book exist."); > > > > if ( pageCounter % 3 =3D=3D 0){ > > mvprintw(++line, 27,"Press any key to see next p= age..."); > > getch(); > > clear(); > > line =3D 1; > > > > } > > pageCounter++; > > refresh(); > > > > fseek(bookFilePtr, counter * sizeof(struct book) ,SEEK_S= ET); // 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 > > --=-4KOWmC1iSKvWODcCMv8h Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit Content-length: 316 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iJwEAAECAAYFAk74fcwACgkQBq6ixBojHB+jSgP/fAVDrfHi87N0VBKooW/0r0tp 1IrD/T1BoNS4NGGINbVzBG2cYjUM9RMxs71g/4QqyxEXoV/M0ZXqx2Ld43cKe/M4 0+sn1fiB0U7wpEeT7osZo9hs05w9wnqhmWNqE5zyHDIxyvThsgw2KFAmbwRfWqlM mFVV2msgiw01Jco9s/o= =2ad1 -----END PGP SIGNATURE----- --=-4KOWmC1iSKvWODcCMv8h--