博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C与C++中IO流的比较
阅读量:4223 次
发布时间:2019-05-26

本文共 4977 字,大约阅读时间需要 16 分钟。

 

    Istream中一些非常用但很有用的函数。

    我们常使用>>, getline(),但是其实istream这个类型是提供了很多的其他很有用的函数。

    我们知道使IO行为成为一种流的方式最本质的特征就是模拟数据向水一样从一个地方流像另外一个地方,所以我们使用>>, getline()操作的时候,就好像前面被操作的数据没有了。其实像istream这样的对象时刻维护着一个指针(streampos),它一般随着对该流的操作而变动,这样就给我们提供了向流水一样的数据传输,但实际上只是有个指针在作用而已。

     有了这个概念我们就知道了,在IOstream library中,有streamoff,streampos,streamsize,这样的int类型的类型来特别表达有关流的当前操作位置以及长度问题。然后我们知道我们可以通过这些对象给与的一些函数做一些相应的我们要做的是,改变流当前位置。

 (1)Streamsize  gcount(),返回前一次操作的长度。

(2)取得相应个数的字符

int get();一个字符

istream& get ( char& c );一个字符,存储在c中,返回已经移动了的istream

istream& get ( char* s, streamsize n );n字符,结束字符\n(结束字符不会被取出。

istream& get ( char* s, streamsize n, char delim );结束字符delim

istream& get ( streambuf& sb);

istream& get ( streambuf& sb, char delim );

(3

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

 

(4)去除n字符(取出,丢弃)

istream&  ignore ( streamsize n = 1, int delim = EOF );

(5)

int peek ( );

与get不同在于streampos不会发生变化。

(6)退回一个stream指针streampos
istream& putback ( char c );
istream& unget ( );
(7)读n个字符,(与get的区别)。
istream& read ( char* s, streamsize n );
streamsize readsome ( char* s, streamsize n );
(8)设置streampos
istream& seekg ( streampos pos );
istream& seekg ( streamoff off, ios_base::seekdir dir );

value

offset is relative to...

ios_base::beg

beginning of the stream buffer

ios_base::cur

current position in the stream buffer

ios_base::end

end of the stream buffer

(9)返回当前streampos常用来,计算长度。

streampos tellg ( );
(10)int sync ( );
 
 
可以看出,只有>>, get, read, getline(),这样的函数用于流数据,peek也是知识get pointer不会变。
 
操作get pointer的有gcount(),ignore,putback,unget,seekg,tellg。

对应istream,ostream也有一类函数共使用。

 (1)       put(对应于get)

ostream& put ( char c );
注意在输出流中,那个位置指针时put point。指针指向的是输出目的文件接受字符的开始位置,要不然会将前面的字符覆盖。
(2)  write(对应于read)
ostream& write ( const char* s , streamsize n );
write没有那么丰富的重载,这表明从一个文件复制数据到另外一个文件只能使用字符数做中间转接。
(3)  seekp,tellp,(对应seekg,tellg)g表示get,p表示put。
(4)  flush,特殊方法,将缓冲区中的数据写入目的流。
 

 

如上所说的这些操作,集中在流的get pointer的操作。一句话,对流的操作不会真的将数据流走,只是get pointer的变动而以。

 

输入输出流会有一个streambuf对象,这个东西非常重要,这些流数据的缓冲地,很多流操作都是以这个对象的操作为基础的!

  

 

有人说cstdio,与iostream是对应的,对的,但是他们两之间只是从功能上对应的,实现的方法上确实相差太多了。

 

Functions

Operations on files:

Remove file (function )

 

Rename file (function )

 

Open a temporary file (function)

 

Generate temporary filename (function)

File access:

Close file (function)

 

Flush stream (function)

 

Open file (function )

 

Reopen stream with different file or mode (function )

 

Set stream buffer (function )

 

Change stream buffering (function )

Formatted input/output:

Write formatted output to stream (function)

 

Read formatted data from stream (function )

 

Print formatted data to stdout (function )

 

Read formatted data from stdin (function )

 

Write formatted data to string (function)

 

Read formatted data from string (function )

 

Write formatted variable argument list to stream (function)

 

Print formatted variable argument list to stdout (function)

 

Print formatted variable argument list to string (function)

Character input/output:

Get character from stream (function)

 

Get string from stream (function )

 

Write character to stream (function)

 

Write string to stream (function)

 

Get character from stream (function)

 

Get character from stdin (function)

 

Get string from stdin (function )

 

Write character to stream (function)

 

Write character to stdout (function)

 

Write string to stdout (function)

 

Unget character from stream (function)

Direct input/output:

Read block of data from stream (function)

 

Write block of data to stream (function)

File positioning:

Get current position in stream (function)

 

Reposition stream position indicator (function)

 

Set position indicator of stream (function)

 

Get current position in stream (function)

 

Set position indicator to the beginning (function)

Error-handling:

Clear error indicators (function)

 

Check End-of-File indicator (function)

 

Check error indicator (function)

 

Print error message (function)

 

Macros

End-of-File (constant)

 

Maximum length of file names (constant)

 

Null pointer (constant)

 

Number of temporary files (constant)

And also , , , , , , , and , each described with its corresponding function.

Types

Object containing information to control a stream (type )

 

Object containing information to specify a position within a file (type)

 

Unsigned integral type (type)

 

可以看到,在C中由于没有类的概念,顶多只有type,于是所有的方法都是纯方法,并没有类对象的依托,这是cstdio,与iostream.fstream.sstream之间的区别。

不过,cstdio仍然为我们提供了一些非常有用和底层的函数。

 

  

 (1)前面介绍iostream时我们说再C IO中,只有对设备和文件的两种IO。是的。于是首先操作文件是cstdio的一个很大部分的功能,这里操作文件与使用流的方式读文件的数据无关。

包括删除、新建、重命名等等。

(2)格式化输入输出printf,scanf这样的函数则是我们常见的一种输入输入方法,格式一词体现了他们与纯输入输出(只是将数据都出来,以字节为单位)。

Printf,sanf这样这是一个特殊的专门用于给stdin和stdout输入输出。

Fprintf,fsanf这样的则是为一般的刘进行输入输出的。

Sprint,ssanf这样的这时面向字符串的输入输出(或许这里就是C++建立string流的灵感)。

还有vfprintf,vspintf,vprintf。这样的可以写一个列表的。

(3)除了上面对各种类型数据的格式化输入输出,还有针对字符串输入输出。

其中的函数基本与格式化输入输出对应。

 

(4)非格式化的,也就是直接写读的方法fread,fwrite。

这些方法其实都是针对文件的(文件包括设备),所以以stdio中以f开头的都是这一类,它们将文件看成一种流的格式(  不想C++那样需要特意声明出一个流对象)。

(5)与C++中一样,流是一种错觉,只是安排了某种特殊的指使位置来做到这一点,在C++中这种指示叫着,get pointer,在C中叫着position。

(6)与C++中流对象的三种状态(eof,bad,fail)以及状态操作对应,C中也有类似的方法来操作流的状态。

转载:

你可能感兴趣的文章
cocos2dx 2.2.6编译记录(1)
查看>>
makefile学习网站
查看>>
C 编写lua模块(1)
查看>>
Lua教程:Lua调用C/C++函数(4)
查看>>
win下创建win32控制台工程,执行lua脚本
查看>>
cocos2dx android启动错误
查看>>
eclipse: android rename package name
查看>>
cocos2dx c++调用java思想
查看>>
cocos2dx lua Node节点 私有数据存取
查看>>
lua math.ceil math.ceil
查看>>
cocos2dx CCNode计算node的大小
查看>>
cocos2dx 布局记录(1)
查看>>
lua 多行注释和取消多行注释
查看>>
缩放系数计算
查看>>
cocos2dx --- 按钮点击居中放大
查看>>
cocos2dx menu位置计算
查看>>
cocos2dx资源加载机制(同步/异步)
查看>>
cocos2dx C++调用java -- 字符串传递
查看>>
git学习网站
查看>>
JavaScript 学习网站
查看>>