diagrama de flujo de E/S
Sin búfer: archivo IO
Ejecutar directamente
baja eficiencia
Agregar búfer: E/S estándar
Reducir los tiempos de IO
avanzado
Cómo los programas usan IO
la diferencia
标准IO:
1.标准IO是由库函数系统提供的,由ANSI C标准定义
2.是带缓冲区的操作,运行效率较高
3.支持跨平台的
4.流指针实现
文件IO:
1.文件IO是由操作系统提供的,由POSIX(可移植操作系统接口)定义
2.没有缓冲区,运行效率没有标准IO高
3.不支持跨平台的
4.文件描述符实现
Implementación de programa
descriptor de archivo
Llamada al sistema, archivo IO
La esencia es el subíndice de matriz.
Puntero al archivo de archivo
puntero de flujo
Funciones de biblioteca, IO estándar
Una estructura: ARCHIVO
Puntero de búfer + puntero de archivo
E/S estándar
área de caché
Flush: confirma el contenido del buffer en el archivo
totalmente amortiguado
Actualizar sólo cuando el buffer esté lleno o forzar la actualización (fflsh)
búfer de línea
Si encuentra\n, actualice primero
Normalmente printf va seguido de \n
Para evitar el bucle infinito del área de búfer temporal, nunca se genera ninguna salida.
Sin almacenamiento en búfer
Actualizar directamente
perror, sin almacenamiento en búfer, actualización directa: se puede actualizar directamente
Abrir y cerrar archivos
abrir
cerrar
定义
FILE *fopen(const char *path, const char *mode);
包
#include <stdio.h>
参数
const char *path
文件路径
const char *mode
打开模式
返回值
EFIL结构体指针
modo abierto
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the
file, but output is always appended to the end of the file.
Código de muestra
int main(int avgc,char *avgc){
FILE *f = fopen(avgc[1],'r');
fclose(f);
}
Archivos de operación
Lectura y escritura de personajes.
Lectura y escritura de filas.
Estructurar la lectura y la escritura.
Escritura formateada
ArchivoIO
Interfaz subyacente del sistema operativo
#include <stdio.h>
int main(int avgc,char *argv[]){
int fr = open(argv[1],O_RDONLY);
int fw = open(argv[2],O_WRONLY||);
}