| | |
Read a string from a binary file into a C or C++ string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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
Ulteo - Taste a bit of freedom
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?
But most of all, could you post your code?
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
char buf[11]; binfile.read(buff, 10) ; // null-terminate the string. This assumes that the // length of the string is known -- 10 buf[10] = 0; // now create the c++ string object 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.
![]() |
Similar Threads
- read second line or record of csv file (PHP)
- Binary File IO (C#)
- Reading a Binary file using Visual Basic (Visual Basic 4 / 5 / 6)
- C# newbie. Read String and Parse. (C#)
- Need help with Binary files and data types in VB6 (Visual Basic 4 / 5 / 6)
- Reading a Binary File to a C++ Class (C++)
- how can i implement a string from Jtextfield and saving it to a file (Java)
Other Threads in the C++ Forum
- Previous Thread: Prompt window size
- Next Thread: Is there anyway to add wav file or any other music file in C programming?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






