Home

Hi,


"ConX." <constantinos@akyroi.gr> wrote in message
news:1189254589.031875.226630@k79g2000hse.googlegroups.com...
> Hello,
>
> Thank you a lot for you response .
>
> I executed your script and the result was exactly what you told me
> will be. My OS is Windows XP and I couldn't read the second file with
> Notepad.
>
> To be honest I didn't understand the difference, you are creating two
> files both in binary mode. At the first you use the stream output and
> at the second one the class member function "write", does this make
> the difference?

Yes, the write is what really makes it a 'binary' file from end-user
perspective.

The second (the binary file) can't be read with a text editor because the
long is stored on disk as it is stored in memory (i.e. as 4 bytes with the
decimal value 123456 encoded binary in them)..

On windows when doing this the result is the same. When you however change
the type to text two things happens '\n' gets translated int CR/LF pair and
there will be a EOF character at the end of file (from the top of my head
0x1A or 26 or Ctrl-Z ).

I usually stick to ios_base::binary since it allows you to write text
formatted files as well but prevents the OS from doing any changes in the
background.


>
> "reinterpret_cast<char*>( &Value )"
> Also I don't understand that part of the 9'th line.
> Could you please help me clarify this issue, it is something I am
> trying for a long time.
>
> Thank you again.
>
> Best Regards,
> Xanthopoulos Constantino
> Akyroi.Gr
>
>
>
> On Sep 8, 2:36 pm, "Ron AF Greve" <ron@localhost> wrote:
>> Hi,
>>
>> Use fstream whenever you can. However you might still need to learn the
>> open/read/write etc functions because on some OS'es they allow you to do
>> other stuff like locking etc.
>>
>> For text/binary. On unix type of systems there usually isn't a difference
>> between text or binary (use binary however to make your code portable).
>> On
>> MS-Windows it does make a difference when you write to a binary file or a
>> text file.
>>
>> To write for instance a long value as text (to a file either in binary or
>> text mode)
>>
>> #include<fstream>
>> using namespace std;
>> main()
>> {
>> long Value = 123456;
>> ofstream Stream( "Test", ios_base::binary ) ;
>> Stream << Value ;
>> ofstream StreamBin( "Testbin", ios_base::binary );
>> StreamBin.write(reinterpret_cast<char*>( &Value ), sizeof(
>> Value ) )
>> ;
>>
>> }
>>
>> Will create a file Test of 6 characters. and a file Tesbin of 4 (assuming
>> a
>> 32 bit long) on unix using text mode it would give the same result.
>>
>> Regarrds, Ron AF Greve
>>
>> http://www.InformationSuperHighway.eu
>>
>> "ConX." <constanti...@akyroi.gr> wrote in message
>>
>> news:1189248192.726547.76550@r29g2000hsg.googlegroups.com...
>>
>> > Hello to all,
>> > I am student at the University of Peireus and we are learning C/C++ at
>> > the first and second semester. At the second semester we are learning
>> > how to handle files.
>>
>> > We are in a little bit confuse here, for many things and we are hoping
>> > that you are able to help us.
>>
>> > I will try to organize my thoughts with an ordered list to make it
>> > more easy for you to answer me.
>>
>> > 1. We don't know which library is more commonly used by the world wide
>> > programmers society. Our professor has tough us about stdio and some
>> > other ways that he calls them "low level" (ex. read, open, _open etc)
>> > which are difficult to use. But as we can see the most programmers use
>> > fstream.
>> > Really what do you suggest us to learn?
>>
>> > 2. Now as we are studying for the exams we are confused with binary
>> > files. We are compiling some scripts and as we read the file we they
>> > have created we see exactly the same result either we have set it in
>> > text or binary mode.
>> > How are we able to see that we achieved to create a binary file?
>>
>> > Thats all for now!
>> > Thank you all in advanced for your answers.
>> > Best Regards,
>> > Xanthopoulos Constantinos
>> > For Akyroi.Gr
>

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

previous
next

Re: Numeric command-line options vs. negative-number arguments
Re: how to initialize list in a vector
Re: help please - newbie learning c++
Re: comparing elements of a list with a string
Re: ordering container contents using more than one key
Fundacja Hobbit
Fundacja Avalon
Mimo Wszystko
Pajacyk
Fundacja Sloneczko