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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 21
Reputation: fesago90 is an unknown quantity at this point 
Solved Threads: 1
fesago90's Avatar
fesago90 fesago90 is offline Offline
Newbie Poster

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

 
0
  #1
Jan 20th, 2007
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.
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #2
Jan 21st, 2007
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?
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,356
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #3
Jan 21st, 2007
Originally Posted by fesago90 View 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.
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.
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC