Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Saving a binary file into a string

Reply
Thread Tools

Re: Saving a binary file into a string

 
 
Rune Allnor
Guest
Posts: n/a
 
      12-27-2009
On 27 Des, 16:39, Dominik Schmidt <(E-Mail Removed)> wrote:
> Hi,
>
> I'm new to C++, so I have a very basic question.
> I wrote a function which opens a file and saves it into a string variable.
> Another function can save a string variable into a file.
> I tried to combine those two functions, because in combination they should
> make an exact copy of a file. And it seems to work, in every case the copy
> had the exact same hash value (MD5) as the original file.
>
> I'd like to know if I made an error, so if there's any possibility that my
> functions won't work in some case?


There is the possibility, yes.

Some byte values or sequences of byte values take on special
meanings like 'end of line' or 'end of string' in the context
of text files and strings.

If you want to work with binary files, use std::vector<char>
or something like that, instead of std::string. That way all
characters are treated as arbitrary numbers, with no special
significance attached to any of them.

Rune
 
Reply With Quote
 
 
 
 
Rune Allnor
Guest
Posts: n/a
 
      12-27-2009
On 27 Des, 19:11, Dominik Schmidt <(E-Mail Removed)> wrote:

> > If you want to work with binary files, use std::vector<char>
> > or something like that, instead of std::string. That way all
> > characters are treated as arbitrary numbers, with no special
> > significance attached to any of them.

>
> Actually I thought my code would already do this by using "ios::binary" in
> both functions?


It might have done, but by using std::string you confuse readers
of your code about what you are doing. std::string is intended
for human-readable text, and anywhere one sees it, one expects
to handle human-readable text. You break with that expectation,
which will only cause confusion later on. You also expose your
data to functions and manipulations intended for strings.

If you use std::vector<char> you first of all make it clear to
the reader that these are binary data, and the reader of your
code will relate to your code accordingly. Secondly you prevent
your data from being accessed through string-manipulating
functions. Which would only have caused mayhem and misery,
if it occured.

The key to C++ is to express the data in terms of the type
or class that best represents the properties of the data.

Since you do *not* have text data, std::string, which is
intended for precisely text data, is the *wrong* container
to use.

Rune
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-28-2009
On Dec 27, 6:11 pm, Dominik Schmidt <(E-Mail Removed)> wrote:
> On Sun, 27 Dec 2009 09:39:12 -0800 (PST), Rune Allnor wrote:


[...]
> > If you want to work with binary files, use std::vector<char>
> > or something like that, instead of std::string. That way all
> > characters are treated as arbitrary numbers, with no special
> > significance attached to any of them.


> Actually I thought my code would already do this by using
> "ios::binary" in both functions?


It should. There could still formally be a problem, since
formally, implementations aren't required to recognize the end
of file correctly for binary files; e.g. you might read 128
bytes, when the input really only contained 10. But in
practice, systems where this would occur are almost non-existent
today.

--
James Kanze
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      01-15-2010
In message
<(E-Mail Removed)>,
Rune Allnor <(E-Mail Removed)> writes
>On 27 Des, 16:39, Dominik Schmidt <(E-Mail Removed)> wrote:
>> Hi,
>>
>> I'm new to C++, so I have a very basic question.
>> I wrote a function which opens a file and saves it into a string variable.
>> Another function can save a string variable into a file.
>> I tried to combine those two functions, because in combination they should
>> make an exact copy of a file. And it seems to work, in every case the copy
>> had the exact same hash value (MD5) as the original file.
>>
>> I'd like to know if I made an error, so if there's any possibility that my
>> functions won't work in some case?

>
>There is the possibility, yes.
>
>Some byte values or sequences of byte values take on special
>meanings like 'end of line' or 'end of string' in the context
>of text files


Yes. But the OP is using binary files, so that's irrelevant.

>and strings.


No. std::string can contain arbitrary sequences of characters without
any constraints on their values, and doesn't impose any semantics on
them.

(The only potential problem is that '\0' is treated as a terminator when
assigning or constructing std::strings from C-style null-terminated
strings, i.e. when calling member functions or operators which take a
single const char * argument. The corresponding functions which take two
iterators (or pointers) don't share this problem.)

>
>If you want to work with binary files, use std::vector<char>
>or something like that, instead of std::string.


Probably good advice, since it makes the intent clearer, but not for the
stated reason:

> That way all
>characters are treated as arbitrary numbers, with no special
>significance attached to any of them.
>


--
Richard Herring
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Saving a binary file into a string Jorgen Grahn C++ 4 12-29-2009 11:14 PM
Re: Saving a binary file into a string Kaz Kylheku C++ 1 12-28-2009 01:09 PM
Re: Saving a binary file into a string Jonathan Lee C++ 1 12-27-2009 09:06 PM
EXCEL question saving a file saving the the first column as read only Luis Esteban Valencia ASP .Net 0 01-06-2005 07:02 PM
Saving a binary file help. G-Factor C++ 7 07-07-2003 04:15 PM



Advertisments