What's the difference between File (File* pointer)stream in C and iostream in C++?
在C和iostream中,文件(文件*指针)流与c++的区别是什么?
Why are they both called stream, do they have something in common?
为什么都叫流,它们有什么共同之处吗?
7 个解决方案
#1
9
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.
区别主要在于c++的iostream对象是类/对象,而C文件数据是通过定义的f*()函数访问的。
So basically, same task, different style of interface.
所以基本上,相同的任务,不同的界面风格。
#2
5
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).
它们都是典型的缓冲,这意味着流上的I/O与底层系统对象(例如文件)上的I/O不匹配。
For instance a fread()
call to read 2 bytes could attempt to read 1,024 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.
例如,对read 2 bytes的fread()调用可以尝试从文件中读取1024个字节,如果您离文件的末尾那么近,那么该文件可能只返回57个字节。差异都隐藏在流实现中,它将返回2个字节,记住is的缓冲区中还有55个字节。因此,它将满足下一个读请求,而不会触及文件级别。
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.
编辑:值得指出的是,两种语言的默认错误流(分别为stderr和cerr)都没有缓冲。这是一个巨大的胜利,因为它增加了你及时输出的机会。当使用printf()语句填充代码来跟踪奇怪的崩溃bug时,出于这个特殊原因,最好将其打印到错误流中。
#3
3
This article gives a good overview of the different output stream available to you in C++.
本文很好地概述了c++中可用的不同输出流。
http://accu.org/index.php/journals/1539
http://accu.org/index.php/journals/1539
It compares:
比较:
- FILE
- 文件
- std::stream
- std::流
- Boost.Format
- Boost.Format
- FastFormat(done by the Author of the Article Matthew Wilson who wrote the Imperfect C++ book.)
- FastFormat(作者是Matthew Wilson,他写了一本不完美的c++书。)
#4
2
Both are different interfaces for OS I/O subsystem.
它们都是OS I/O子系统的不同接口。
#5
1
C++ Streams are extensible in two ways that C files are not:
c++流的可扩展性有两种方式,而C文件没有:
- 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.
- 如果为一个类定义了流操作符,那么该类的任何对象都可以被写入并从任何流中读取。
#6
0
compare file stream between C and Java
比较C和Java之间的文件流。
#7
-1
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.
流通常用于引用未格式化的原始数据,这些数据只是二进制字节的一部分。考虑一下复制到新分配内存的文件内容,在解析它们之前(也就是它们被复制之后的瞬间),它们对您来说只是二进制字节的一部分。所以您只有像seek()这样的方法来访问它们,它基于字节。
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.
将其与您可以ReadLine()、WriteLine()的文本文件进行比较,后者在预格式化实体中工作(在本例中称为行)。你会懂的。
#1
9
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.
区别主要在于c++的iostream对象是类/对象,而C文件数据是通过定义的f*()函数访问的。
So basically, same task, different style of interface.
所以基本上,相同的任务,不同的界面风格。
#2
5
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).
它们都是典型的缓冲,这意味着流上的I/O与底层系统对象(例如文件)上的I/O不匹配。
For instance a fread()
call to read 2 bytes could attempt to read 1,024 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.
例如,对read 2 bytes的fread()调用可以尝试从文件中读取1024个字节,如果您离文件的末尾那么近,那么该文件可能只返回57个字节。差异都隐藏在流实现中,它将返回2个字节,记住is的缓冲区中还有55个字节。因此,它将满足下一个读请求,而不会触及文件级别。
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.
编辑:值得指出的是,两种语言的默认错误流(分别为stderr和cerr)都没有缓冲。这是一个巨大的胜利,因为它增加了你及时输出的机会。当使用printf()语句填充代码来跟踪奇怪的崩溃bug时,出于这个特殊原因,最好将其打印到错误流中。
#3
3
This article gives a good overview of the different output stream available to you in C++.
本文很好地概述了c++中可用的不同输出流。
http://accu.org/index.php/journals/1539
http://accu.org/index.php/journals/1539
It compares:
比较:
- FILE
- 文件
- std::stream
- std::流
- Boost.Format
- Boost.Format
- FastFormat(done by the Author of the Article Matthew Wilson who wrote the Imperfect C++ book.)
- FastFormat(作者是Matthew Wilson,他写了一本不完美的c++书。)
#4
2
Both are different interfaces for OS I/O subsystem.
它们都是OS I/O子系统的不同接口。
#5
1
C++ Streams are extensible in two ways that C files are not:
c++流的可扩展性有两种方式,而C文件没有:
- 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.
- 如果为一个类定义了流操作符,那么该类的任何对象都可以被写入并从任何流中读取。
#6
0
compare file stream between C and Java
比较C和Java之间的文件流。
#7
-1
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.
流通常用于引用未格式化的原始数据,这些数据只是二进制字节的一部分。考虑一下复制到新分配内存的文件内容,在解析它们之前(也就是它们被复制之后的瞬间),它们对您来说只是二进制字节的一部分。所以您只有像seek()这样的方法来访问它们,它基于字节。
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.
将其与您可以ReadLine()、WriteLine()的文本文件进行比较,后者在预格式化实体中工作(在本例中称为行)。你会懂的。