944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 18083
  • C++ RSS
Jan 20th, 2007
0

Read a string from a binary file into a C or C++ string

Expand Post »
Hi, just looking for a quick answer to my problem:
How can I read a string from a binary file?
for example

string buff;
binfile.read(reinterpret_cast<char*>(&buff), 10)

does not work. Doesn't work either if buff is declared as a c-style string.
I'd preferably like to read it into an STL string. Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fesago90 is offline Offline
21 posts
since Jan 2007
Jan 21st, 2007
0

Re: Read a string from a binary file into a C or C++ string

If it doesn't work when using a C-style string, there's obviously something fishy going on in your code. Perhaps you're trying to read from the wrong location. As usual, check that the stream is not in an error state, and check the number of charecters read if an error occured.

But most of all, could you post your code?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 21st, 2007
0

Re: Read a string from a binary file into a C or C++ string

Click to Expand / Collapse  Quote originally posted by fesago90 ...
Hi, just looking for a quick answer to my problem:
How can I read a string from a binary file?
for example

string buff;
binfile.read(reinterpret_cast<char*>(&buff), 10)

does not work. Doesn't work either if buff is declared as a c-style string.
I'd preferably like to read it into an STL string. Thanks.
Of course it doesn't work -- you are passing a pointer to a c++ object to a function that wants a char*. And you are fooling the compiler into thinking that YOU are correct, which you are not.
C++ Syntax (Toggle Plain Text)
  1. char buf[11];
  2. binfile.read(buff, 10) ;
  3. // null-terminate the string. This assumes that the
  4. // length of the string is known -- 10
  5. buf[10] = 0;
  6. // now create the c++ string object
  7. string str = buf;

You have to know the contents of the binary file for the above to work -- if the file contains variable-length strings then the file must also contain something that indicates the length of the string, such as preceeding the string with its length, or the file contains a null character that indicates the end of the string. Either of these conditions makes your program a little more complex.

The code above should work if the strings are fixed-length strings, in the example the length of the string would always be 10, if shorter than 10 then the string in the file would have to be padded with spaces to force it to 10 characters.

Without knowing more about the contents of the binary file you are working with it is nearly impossible to give you more definitive help.
Last edited by Ancient Dragon; Jan 21st, 2007 at 9:03 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,960 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Prompt window size
Next Thread in C++ Forum Timeline: Is there anyway to add wav file or any other music file in C programming?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC