From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6842 invoked by alias); 25 Dec 2011 22:14:09 -0000 Received: (qmail 6830 invoked by uid 22791); 25 Dec 2011 22:14:08 -0000 X-SWARE-Spam-Status: No, hits=-0.9 required=5.0 tests=AWL,BAYES_50,RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from www.courier-mta.com (HELO www.courier-mta.com) (216.254.115.190) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 25 Dec 2011 22:13:56 +0000 Received: from monster.email-scan.com (monster.email-scan.com [::ffff:192.168.0.2]) (TLS: TLSv1/SSLv3,256bits,AES256-SHA) by www.courier-mta.com with ESMTPS; Sun, 25 Dec 2011 17:13:54 -0500 id 00000000000A0DF0.000000004EF7A022.000007C4 Received: from localhost (localhost [127.0.0.1]) (uid 8) by monster.email-scan.com with local; Sun, 25 Dec 2011 17:13:54 -0500 id 00000000000CA071.000000004EF7A022.00004288 X-IMAP-Sender: mrsam.virtual References: <1324861286.2539.24.camel@debian> Message-ID: From: Sam Varshavchik To: gcc-help Subject: Re: string in structure and fread() Date: Sun, 25 Dec 2011 23:05:00 -0000 Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=_mimegpg-monster.email-scan.com-13454-1324851234-0001"; micalg=pgp-sha1; protocol="application/pgp-signature" 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/msg00230.txt.bz2 This is a MIME GnuPG-signed message. If you see this text, it means that your E-mail or Usenet software does not support MIME signed messages. The Internet standard for MIME PGP messages, RFC 2015, was published in 1996. To open this message correctly you will need to install E-mail or Usenet software that supports modern Internet standards. --=_mimegpg-monster.email-scan.com-13454-1324851234-0001 Content-Type: text/plain; format=flowed; delsp=yes; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 7bit Content-length: 2233 Mohsen Pahlevanzadeh writes: > short unsigned size = 100 - strlen((const char *)name.data()); std::string::data() is not guaranteed to be null-terminated. Undefined behavior. Furthermore, this is overdoing it. name.size() will work much better. > memory = '`'; > for (i =0 ;i memory += '`'; > tmp->name = name + memory; You should replace all of the above effort with a simple: tmp->name = name; tmp->name.resize(100, '`'); > > size = 100 - strlen((const char *)publisher.data()); > memory = '`'; > for (i =0 ;i memory += '`'; > tmp->publisher = publisher + memory; Same here. tmp->publisher=publisher; tmp->publisher.resize(100, '`'); > fseek(bookFilePtr,0L,SEEK_END); > fwrite(ptr,408,1,bookFilePtr); Undefined behavior. std::string is not an char[]. It is a structure. Hint: if you take a look at what sizeof(ptr) tells you, it's not going to be 408 bytes. > while ( fread(bookPtrObj,408,1,bookFilePtr) == 1 ){ > > But i get the segmentation fault on read each field. fread() and fwrite() read and write a raw char buffer. char[]. std::string is not a char[]. Internally, std::string is a single pointer to somewhere else in memory, where std::string happens to have dumped the current contents of your string. If you fread() and fwrite() the std::string itself, fread() and fwrite() knows nothing about what it's reading or writing, besides the actual bytes themselves. fread() and fwrite() has no knowledge that those bytes consist of a pointer to somewhere else in memory, where a string is. If you actually look at what your fwrite() written out, it's binary garbage. To do this properly, you have to manually place everything you want to write out into a single char buffer. std::vector would be an excellent choice: std::vector buffer; Then, after you filled this buffer with whatever you want to write: fwrite(&buffer[0], buffer.size(), 1, fp); But, if you really want to use C++, rather than C, you should use std::ofstream, instead of FILE *. Then to read what you previous wrote out, you have to allocate the buffer first, using resize(), then you can read what you want to read, into the newly-allocated buffer. --=_mimegpg-monster.email-scan.com-13454-1324851234-0001 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit Content-length: 198 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEABECAAYFAk73oCIACgkQx9p3GYHlUOKZUwCeI92XiaKGgZ+s0p6IIpH0C5Uy 5mQAn0paq3UxqNcUk6clwCn1O7d4YJsd =t5E8 -----END PGP SIGNATURE----- --=_mimegpg-monster.email-scan.com-13454-1324851234-0001--