C-style file input/output

The CI/O subset of the C++ Standard Library implements C-style stream input/output operations. <cstdio>The header files provide general file support and provide functions with narrow and multibyte character I/O capabilities, while the <cwchar>header files provide functions with wide character I/O capabilities.

The C stream is an object of type std::FILE , which can only be accessed and manipulated through pointers of type std::FILE* (note: when it is possible to dereference legal std::FILE*, copy and create a local object of type std::FILE , using the address of such a copy in an I/O function is undefined behavior). Each C stream is associated with an external physical device (file, standard input stream, printer, serial port, etc.).

C streams can be used for unformatted and formatted input and output. They are locale sensitive and provide wide/multibyte conversion when needed. Unlike a C++ stream, which is associated with its own locale, all C streams access the same locale object: the one installed by the most recent std::setlocale.

In addition to system-qualified information necessary to access the device (such as POSIX file descriptors), each C stream object holds the following:

1) Character width: not set, narrow or wide.

2) Buffering status: no buffering, line buffering, full buffering.

3) Buffer, which can provide buffer replacement for external users.

4) I/O mode: input, output or update (both input and output).

5) Binary/text mode indicator.

6) End of file indicator.

7) Error status indicator.

8) A file position indicator ( std::fpos_tobject of type std::mbstate_t ), for wide character streams containing the dissecting state (object of type std::mbstate_t ).

9) (C++17) A reentrant lock that avoids data races when multiple threads read, write, seek, or query a stream.

narrow and wide

Newly opened streams are unoriented. The first call to std::fwide or an I/O function establishes orientation: wide I/O functions make the stream wide-oriented, narrow I/O functions make the stream narrow-oriented. Once set, the orientation can only be changed with std::freopen. Narrow I/O functions cannot be called on wide-oriented streams, and wide I/O functions cannot be called on narrow-oriented streams. The wide I/O functions convert between wide and multibyte characters as if calling std::mbrtowc and std::wcrtomb. Unlike legal multibyte character strings in programs, multibyte characters in files may contain embedded null characters and do not have to start or end with an initial transition state.

POSIX requires that when the orientation of a stream object becomes wide, the LC_CTYPE plane of the currently installed C locale is stored in it, and that it is used for all future I/O on this stream until the orientation is changed, regardless of any std A subsequent call to ::setlocale.

Binary vs Text Mode

A text stream is an ordered sequence of characters that are composed (zero or more characters plus a terminating '\n'). Whether the final line requires a terminating '\n' is implementation-defined. Characters may have to be added, switched, or removed on input and output to conform to the representation text in the OS (in particular C streams on Windows OS convert to on output and to \non \r\ninput \r\n) \n.

Data read from a text stream is guaranteed to compare equal to those previously written to that text stream only if all of the following conditions are true:

  • Data consists only of printing characters and control characters \tand \n(especially on Windows OS, the character '\0x1A'terminates input)
  • No \n immediately preceded by whitespace (whitespace written immediately before \n may disappear on read)
  • The trailing character is \n

A binary stream is an ordered sequence of characters that can transparently record internal data. Data read from a binary stream always compares equal to those previously written to that stream. Implementations are only allowed to append some null character to the end of the stream. Wide binary streams do not have to terminate at the initial transition state.

The POSIX implementation does not distinguish between text and binary streams (no special mapping for \n or any other characters).

function

defined in the header file<cstdio>

file access

fopen

open file
(function)

freopen

open an existing stream
(function) with a different name

fclose

close file
(function)

fflush

Synchronize the output stream with the actual file
(function)

fried

switch file stream between wide character I/O and narrow character I/O
(function)

setbuf

set buffer for file stream
(function)

setvbuf

Set the buffer and its size for a file stream
(function)

 

direct input/output

fread

read from file
(function)

fwrite

write to file
(function)

 

Unformatted I/O

byte/multibyte character

fgetcgetc

Get characters from a file stream
(function)

fgets

Get a string from a file stream
(function)

fputcputc

write characters to a file stream
(function)

fputs

write string to file stream
(function)

getchar

read characters from stdin
(function)

gets

(deprecated in C++11)(removed in C++14)

read a string from stdin
(function)

putchar

write characters to stdout
(function)

puts

写字符串到 stdout
(函数)

ungetc

把字符放回文件流
(函数)

宽字符

fgetwcgetwc

从文件流获取宽字符
(函数)

fgetws

从文件流获取宽字符串
(函数)

fputwcputwc

写宽字符到文件流
(函数)

fputws

写宽字符串到文件流
(函数)

getwchar

stdin 读取宽字符
(函数)

putwchar

写宽字符到 stdout
(函数)

ungetwc

把宽字符放回文件流
(函数)

 

有格式输入/输出

字节/多字节字符

scanffscanfsscanf

stdin、文件流或缓冲区读取有格式输入
(函数)

vscanfvfscanfvsscanf

(C++11)(C++11)(C++11)

使用可变实参列表
stdin、文件流或缓冲区读取有格式输入
(函数)

printffprintfsprintfsnprintf

(C++11)

打印有格式输出到 stdout、文件流或缓冲区
(函数)

vprintfvfprintfvsprintfvsnprintf

(C++11)

使用可变实参列表
打印有格式输出到 stdout、文件流或缓冲区
(函数)

宽字符

wscanffwscanfswscanf

stdin、文件流或缓冲区读取有格式宽字符输入
(函数)

vwscanfvfwscanfvswscanf

(C++11)(C++11)(C++11)

使用可变实参列表
stdin、文件流或缓冲区读取有格式宽字符输入
(函数)

wprintffwprintfswprintf

打印有格式宽字符输出到 stdout、文件流或缓冲区
(函数)

vwprintfvfwprintfvswprintf

使用可变实参列表打印
有格式宽字符输出到 stdout、文件流或缓冲区
(函数)

文件寻位

ftell

返回当前文件位置指示器
(函数)

fgetpos

获取文件位置指示器
(函数)

fseek

移动文件位置指示器到文件中的指定位置
(函数)

fsetpos

移动文件位置指示器到文件中的指定位置
(函数)

rewind

移动文件位置指示器到文件起始
(函数)

 

错误处理

clearerr

清除错误
(函数)

feof

检查文件尾
(函数)

ferror

检查文件错误
(函数)

perror

显示对应当前错误的字符串于 stderr
(函数)

文件上的操作

remove

删除文件
(函数)

rename

重命名文件
(函数)

tmpfile

创建并打开一个临时、自动移除的文件
(函数)

tmpnam

返回一个唯一独有的文件名
(函数)

 

类型

类型 定义
FILE 对象类型,足以保有控制 C I/O 流所需的全部信息
fpos_t 完整非数组对象类型,足以唯一指定文件中的位置,包含其多字节剖析状态

size_t

sizeof 运算符返回的无符号整数类型
(typedef)

宏 

stdinstdoutstderr

与输入流关联到 FILE* 类型表达式
与输出流关联的 FILE* 类型表达式
与错误输出流关联的 FILE* 类型表达式
(宏常量)

EOF

拥有 int 类型和负值的整数常量表达式
(宏常量)

FOPEN_MAX

能同时打开的文件数
(宏常量)

FILENAME_MAX

要保有最长受支持文件名的 char 数组所需的长度
(宏常量)

BUFSIZ

std::setbuf 所用的缓冲区大小
(宏常量)

_IOFBF_IOLBF_IONBF

给 std::setbuf 的参数,指示全缓冲 I/O
给 std::setbuf 的参数,指示行缓冲 I/O
给 std::setbuf 的参数,指示无缓冲 I/O
(宏常量)

SEEK_SETSEEK_CURSEEK_END

给 std::fseek 的参数,指示从文件起始寻位
给 std::fseek 的参数,指示从当前文件位置寻位
给 std::fseek 的参数,指示从文件尾寻位
(宏常量)

TMP_MAX

std::tmpnam 所能生成的唯一文件名的最大数量
(宏常量)

L_tmpnam

保有 std::tmpnam 结果的 char 数组所需的大小
(宏常量)

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132630654
Recommended