FIX: C IO

This commit is contained in:
rangersly 2025-04-23 13:36:24 +08:00
parent b95d6bf352
commit ad88fb5b36

View File

@ -2,66 +2,105 @@
# 目录 # 目录
- [c stdio](# c stdio)
- [c fio](# c fio)
- [cpp stdio](# cpp stdio)
- [cpp fio](# cpp fio)
- [关闭缓冲区读取](# ../linux/ncurses.c)
- [标准IO流](# IOstream)
- [C](# C)
- [格式化IO](# 格式化IO)
- [字符IO](# charIO)
- [Cstring](# Cstring)
- [C二进制IO](# Cbin)
- [C文件操作](# C-FILE)
- [error](# c-error)
- [cpp stdio](# cpp stdio)
- [cpp fio](# cpp fio)
- [关闭缓冲区读取](# ../linux/ncurses.c)
- [ANSI](# ./ansi.md)
## c stdio ---
## **IOstream**
+ stdin
+ stdout
+ stderr
---
## **C**
> 头文件
`#include <stdio.h>` `#include <stdio.h>`
> scanf 格式化输入函数 ---
printf 格式化输出函数
### 格式化IO
- scanf 格式化输入函数
- printf 格式化输出函数
- fscanf 格式化输入函数(第一个参数是`FILE *stream`)
- fprintf 格式化输出函数(第一个参数是`FILE *stream`)
__控制符__ __控制符__
+ %d %nd %ld %x %o 整数 + %d %nd %ld %x %o 整数
+ %f %n.mf %lf 浮点数 + %f %n.mf %lf 浮点数
+ %c + %c
+ %s + %s
+ %p ptr + %p ptr
> 文件流 ---
+ stdin
+ stdout
+ stderr
`int getchar(void);` ### charIO
`int putchar(int c);`
`int getc(FILE *stream);` getchar() 文件版 + IN
`int putc(int c, FILE *stream);` |function prototype |remark |
|--------------------------|-------------------------------------|
|`int getchar(void)` |宏,标准IO |
|`int getc(FILE *stream)` |宏,所有流 |
|`int fgetc(FILE *stream)` |真正的函数 |
|`int ungetc(int c,FILE *stream)`|将一个字符返回流中 |
`char *fgets(char *s, int size, FILE *stream);` + OUT
+ 换行符后(包含换行符) |function prototype |remark |
+ 文件尾 |--------------------------|-------------------------------------|
+ size-1 |`int putchar(int c)` |宏,标准IO |
`fputs(const char *s, FILE *stream);` **不添加换行符** |`int putc(int c,FILE *stream)`|宏,所有流 |
|`int fputc(int c,FILE *stream)`|真正的函数 |
---
### Cstring
|function prototype |remark |
|---------------------------------------------------------|--------------------------------|
|`char *fgets(char *s,int size,FILE *stream)` |到达行尾(包含换行符),EOF,size-1返回|
|`int fputs(const char *restrict s, FILE *restrict stream)`|不在后添加换行符 |
---
### Cbin
`size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);` `size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);`
+ size 读写的块大小 + size 读写的块大小
+ nmemb 读写的块数量 + nmemb 读写的块数量
`size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);` `size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);`
### C-FILE
- `FILE *fp` 文件指针
## c fio - `FILE *fopen(const char *pathname, const char *mode);` 错误返回NULL
- `int fclose(FILE *stream);` 关闭文件 返回0为正常关闭
`FILE *fp` 文件指针 - `FILE *tmpfile(void)` 创建一个临时文件,返回一个'wb+'模式的文件指针
- `char *tmpnam(char *name)` 创建一个临时文件名,最长`L_tmpnam`
`FILE *fopen(const char *pathname, const char *mode);` 错误返回NULL - `int remove(const char *pathname)` 删除一个文件
`int fclose(FILE *stream);` 关闭文件 返回0为正常关闭 - `int rename(const char *oldname, const char *newname)` 修改文件名字
**mode** **mode**
+ "r" 读 + "r" 读
+ "w" 写截0新建 + "w" 写截0新建
+ "a" 写,尾+,新建 + "a" 写,尾+,新建
+ "r+" 读写 + "r+" 读写
+ "w+" 读写截0新建 + "w+" 读写截0新建
+ "a+" 读写,仅尾+,新建 + "a+" 读写,仅尾+,新建
+ "b" 搭配表二进制仅win + "b" 搭配表二进制仅win
`long ftell(FILE *stream);` 获取当前文件指针位置 `long ftell(FILE *stream);` 获取当前文件指针位置
`int fseek(FILE *stream, long offset, int whence);` 移动文件指针 `int fseek(FILE *stream, long offset, int whence);` 移动文件指针
@ -73,93 +112,104 @@ __控制符__
`int fflush(FILE *stream);` 刷新缓冲区 `int fflush(FILE *stream);` 刷新缓冲区
`int setvbuf(FILE *stream, char *buf, int mode, size_t size);` 设置缓冲区,return 0 `int setvbuf(FILE *stream, char *buf, int mode, size_t size);` 设置缓冲区,return 0
+ buf 设置的缓冲区 + buf 设置的缓冲区
+ mode + mode
- `_IOFBF` 完全缓冲 - `_IOFBF` 完全缓冲
- `_IOLBF` 行缓冲 - `_IOLBF` 行缓冲
- `_IONBF` 无缓冲 - `_IONBF` 无缓冲
+ size 指定缓冲区的大小,无缓冲时无效 + size 指定缓冲区的大小,无缓冲时无效
`int ferror(FILE *stream);` 流异常时return非0 ---
## c-error
- `void perror(const char *message);`
- 打印message并在后面根据errno错误码生成错误信息
- errno全局变量定义于`<errno.h>`,仅在发生错误时被设置
- `int ferror(FILE *stream);` 流异常时return非0
- `int feof(FILE *stream)` 文件尾时返回真
- `void clearerr(FILE *stream)` 重置错误位
---
## cpp stdio ## cpp stdio
> 头文件 > 头文件
#include <iostream> #include <iostream>
`ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);` 关闭缓冲加速 `ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);` 关闭缓冲加速
> >
```cpp ```cpp
cin.get(c); //返回一个流引用 cin.get(c); //返回一个流引用
c = cin.get(); //返回char略快 c = cin.get(); //返回char略快
cin.get(str, 100, '\n'); //读入字符串 cin.get(str, 100, '\n'); //读入字符串
cin.width(10); //设置场宽 cin.width(10); //设置场宽
``` ```
> >
```cpp ```cpp
std::flush // 立即刷新缓冲区 std::flush // 立即刷新缓冲区
std::endl // 换行加刷新 std::endl // 换行加刷新
``` ```
________________________________________________ ________________________________________________
## file IO ## file IO
> 头文件 > 头文件
#include <fstream> #include <fstream>
```cpp ```cpp
//文件流的创建 打开 关闭 //文件流的创建 打开 关闭
fstream fin; fstream fin;
fstream fout; fstream fout;
fin.open("in"); fin.open("in");
fout.open("out"); fout.open("out");
fin.close(); fin.close();
fout.close(); fout.close();
/* /*
open函数的原型如下 open函数的原型如下
void open(char const *,int filemode,int =filebuf::openprot); void open(char const *,int filemode,int =filebuf::openprot);
它有3个参数第1个是要打开的文件名第2个是文件的打开方式第3个是文件的保护方式一般都使用默认值。 它有3个参数第1个是要打开的文件名第2个是文件的打开方式第3个是文件的保护方式一般都使用默认值。
第2个参数可以取如下所示的值 第2个参数可以取如下所示的值
打开方式 解释 打开方式 解释
ios::in 打开文件进行读操作,这种方式可避免删除现存文件的内容 ios::in 打开文件进行读操作,这种方式可避免删除现存文件的内容
ios::out 打开文件进行写操作,这是默认模式 ios::out 打开文件进行写操作,这是默认模式
ios::ate 打开一个已有的输入或输出文件并查找到文件尾开始 ios::ate 打开一个已有的输入或输出文件并查找到文件尾开始
ios::app 在文件尾追加方式写文件 ios::app 在文件尾追加方式写文件
ios::binary 指定文件以二进制方式打开,默认为文本方式 ios::binary 指定文件以二进制方式打开,默认为文本方式
ios::trunc 如文件存在,将其长度截断为零并清除原有内容,如果文件存在先删除,再创建 ios::trunc 如文件存在,将其长度截断为零并清除原有内容,如果文件存在先删除,再创建
除ios_base::app方式之外文件刚刚打开时当前读写位置的文件指针都定位于文件的开始位置而ios_base::app使文件当前的写指针定位于文件尾 除ios_base::app方式之外文件刚刚打开时当前读写位置的文件指针都定位于文件的开始位置而ios_base::app使文件当前的写指针定位于文件尾
函数 功能 函数 功能
bad() 如果进行非法操作返回true否则返回false bad() 如果进行非法操作返回true否则返回false
clear() 设置内部错误状态,如果用缺省参量调用则清除所有错误位 clear() 设置内部错误状态,如果用缺省参量调用则清除所有错误位
eof() 如果提取操作已经到达文件尾则返回true否则返回false eof() 如果提取操作已经到达文件尾则返回true否则返回false
good() 如果没有错误条件和没有设置文件结束标志返回true否则返回false good() 如果没有错误条件和没有设置文件结束标志返回true否则返回false
fail() 与good相反操作失败返回false否则返回true fail() 与good相反操作失败返回false否则返回true
is_open() 判定流对象是否成功地与文件关联若是返回true否则返回false is_open() 判定流对象是否成功地与文件关联若是返回true否则返回false
*/ */
// 二进制文件读写 // 二进制文件读写
fin.write((char*)&data, sizeof data); fin.write((char*)&data, sizeof data);
fin.read((char*)&data, sizeof data); fin.read((char*)&data, sizeof data);
/* /*
随机存取 随机存取
seekg(pos,ios::); seekg(pos,ios::);
seekg(pos); seekg(pos);
seekp(pos,ios::); seekp(pos,ios::);
seekp(pos); seekp(pos);
tellg(); tellg();
tellp(); tellp();
文件位置 文件位置
ios::beg ios::beg
ios::cur ios::cur
ios::end ios::end
*/ */
``` ```