Some Questions From Greece!!!

C

ConX.

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
 
R

Ron AF Greve

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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?

When writing C++ fstream is generally preferable over the stdin functions.
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?

Nitpick: C++ is not a scripting language so you compile source code or
source files and not scripts.

To write binary files make sure that you set the open-mode to binary:

ifstream file("filename", std::ios_base::binary);

Also notice that for a subset of data there is no difference between
text and binary data, especially when writing one char at a time, if you
instead write integers or float you should see the difference. Also
notice that to read and write binary data you should use the read() and
write() functions, and not the << and >> operators.
 
O

osmium

ConX. said:
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?

As I read your post, you are enrolled in a course in C++, not C/C++. So
your first choice should be fstream. He is probably teaching the C way
because it is easier to learn. Real C++ programmers use fstream.
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?

"Binary file" as used in C++ ( and inherited from C) is a very bad choice of
words. It involves different ways of encoding the '\n' character on the
storage medium. There is a transparent (hidden) translation mechanism that
occurs on some systems. Notably Windows stores '\n' as <LF><CR> (or the
inverse, I can never recall which), externally but obeys '\n' internally.
This presumes the ASCII character set.

Browsing on google *groups* will yield an interminable set of discussions on
this subject.
 
C

ConX.

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?

"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
 
R

Ron AF Greve

Hi,


ConX. said:
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

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
C

ConX.

Hello,

Thank you all for you help!

You are right! Wrong use of English

I searched and found that reinterpret_cast is an Operator to change
the data type into another, is that right? When I have to use it?
Because write function accepts only char?

Best Regards,
Xanthopoulos Constantinos
http://www.akyroi.gr
 
R

Ron AF Greve

Hi,

I forgot the 9'th line explanation.

What you tell the compiler there is that the address of Value &Value (which
would resolve to 'pointer to long' or 'address of long' ) is actually a
pointer to a character (or the starting address of a bunch of characters).
So what it does is telling the compiler to handle it as a bunch of
character. Now the write function also needs to know how many characters it
must expect at that address, this is done by 'sizeof ' which returns the
amount of bytes Value occupies in memory.

Note the difference between the different cast
reinterpret_cast tell the compiler the pointer is a pointer to a different
type (doesn't actually have any effect, the address remains the same
address). i.e. Interpret the address different

static_cast Will convert the object or value to another one, IF the compiler
knows how to do this or you made a operator for this! For instance the built
in float to in
float Float = 1.2;
It might also alter an address, for instance a casting a pointer from a base
to derived and vice versa. i.e. Change the object or pointer into something
different
static_cast<int>( Float ); // would become 1

const_cast just telling the compiler something isn't constant (be always
very careful using this one, it always is suspicious). i.e change const'ness

C-Style cast (type)( value) Kind of a combination of the above.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
R

Ron AF Greve

ConX. said:
Hello,

Thank you all for you help!


You are right! Wrong use of English


I searched and found that reinterpret_cast is an Operator to change
the data type into another, is that right?
Yes that's more or less correct however reinterpret_cast doesn't change but
interprets it differently. Effectively you are telling the compiler the type
is something else (in this case needed because indeed the write function is
expecting a char * while we have a long *)
When I have to use it?
When something expects a different type but you know the type you supply
would do too.
Because write function accepts only char?
Yes. The compiler checks it to prevent you from making mistakes, however in
this case we want to write are long just as a series of bytes so we know it
is ok. But be careful with these things, you are saying to the compiler
'trust me I know what I am doing' :)
Best Regards,
Xanthopoulos Constantinos
http://www.akyroi.gr
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
J

James Kanze

As I read your post, you are enrolled in a course in C++, not C/C++. So
your first choice should be fstream. He is probably teaching the C way
because it is easier to learn. Real C++ programmers use fstream.

Not always. In fact, I find that fstream is fairly rare in my
code (and stdio, of course, completely absent), although
stringstream is fairly frequent. In modern programs, most IO
seems to be to systems not covered by the traditional stream IO:
data bases, sockets and GUIs. (The iostream's do get extended
to handle some of this, of course.)
"Binary file" as used in C++ ( and inherited from C) is a very
bad choice of words.

IMHO, the problem isn't so much the word, as where it appears.
What is "binary" isn't the [io]stream (which is mainly concerned
with formatting), but the type of file. Apply it to filebuf
(the only class which actually uses it), and it fully makes
sense.
It involves different ways of encoding the '\n' character on the
storage medium.

Not only. Fundamentally, C++ (like C) deals with two types of
files: binary and text. On systems where there really is just
one type of file, like Windows or Unix, the differences are
restricted to how '\n' and EOF are encoded. On other systems,
binary and text may use different file types at the system level
(and it may not even be possible to open a file that was written
as binary in text mode, or vice versa).
There is a transparent (hidden) translation mechanism that
occurs on some systems. Notably Windows stores '\n' as
<LF><CR> (or the inverse, I can never recall which),
externally but obeys '\n' internally. This presumes the ASCII
character set.

I believe that some ports of C to IBM mainframes translated from
ASCII to EBCDIC in text mode: internally, C used ASCII (because
so many C programs assumed this), but externally, text files
were EBCDIC.
Browsing on google *groups* will yield an interminable set of
discussions on this subject.

Yes. But for most people, it's sufficient to say that if your
writing text to a local disk, to be read on the same (or a
similar) machine, then you should use text. In all other cases,
you probably want binary. And of course, if the format itself
isn't pure text, you probably don't want iostream at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top