views:

1069

answers:

7

What's the difference between File (File* pointer)stream in C and iostream in C++?

Why are they both called stream, do they have something in common?

+7  A: 

They both deal with files, and "stream" is simply a very general term for data that is coming in pieces from some source.

The difference mainly is that C++'s iostream objects are classes/objects, and C file data is accessed through the defined f*() functions.

So basically, same task, different style of interface.

MattJ
+4  A: 

They are both typically buffered, which means that the I/O you do on a stream does not match 1:1 the I/O done on the underlying system object (e.g. a file).

For instance a fread() call to read 2 bytes could read 1024 bytes from the file, which might in turn only return 57 if you were that close to end of file. The difference is all hidden by the stream implementation, which will return 2 bytes and remember that is has a further 55 in its buffers. It will thus satisfy the next read request without hitting the file level at all.

EDIT: It's worth pointing out that the default error streams in both languages (stderr and cerr, respectively) are not buffered. This is a big win, since it increases the chance of getting your output out in time. When chasing weird crash bugs by sprinkling code with printf() statements, it's a very good idea to print to the error stream for this particular reason.

unwind
Streams are not necessarily buffered; stderr and cerr would lose a good deal of functionality if they had to be. The idea of a stream is that it's a sequence of undifferentiated bytes, and buffering is more of an implementation or OS thing.
David Thornley
True, the error streams are unbuffered. I'll edit.
unwind
+2  A: 

Both are different interfaces for OS I/O subsystem.

bb
what do you mean by "system entity"?
anon
I meant input/output subsystem of operation system.
bb
I've edited answer, thank you.
bb
A: 

it's easier to tell their familiarity than differences, because theirs only one familiarity: they both bear one term(stream) that's common in the programming world.

stream is often used to refer to un-formated raw data which are just a chunk of binary bytes. Think of the file content that's copied to the newly allocated memory, before they are parsed(that is the moment immediately after they are copied over) they are just a chunk of binary bytes to you. So you only have methods like seek() to access them, which works on a basis of bytes.

Compare that to Text Files that you can ReadLine(), WriteLine(), which work in pre-format entities(called lines in this case). You'll get the idea.

t.g.
+1  A: 

C++ Streams are extensible in two ways that C files are not:

  • You can create your own stream type and all streamable objects will automatically work with it.
  • If stream operators are defined for a class, any object of that class can be written to and read from any stream.
Ferruccio
+3  A: 

This article gives a good overview of the different output stream available to you in C++.

http://accu.org/index.php/journals/1539

It compares:

  • FILE
  • std::stream
  • Boost.Format
  • FastFormat(done by the Author of the Article Matthew Wilson who wrote the Imperfect C++ book.)
James Dean
A: 

compare file stream between C and Java