打开
FILE * fpointer;
f = open("path","mode");
模式
modes:
r,w,a
read write append
r+,w+,a+
读写
格式化读写
format read & write
int
fscanf
(FILE* stream, const char* format,[argument])
;
fprintf
(stdout,const char *format,[argument])
;
按字符/字符串读写
int putchar(int c); //
int putc(int c,FILE *fp); //宏实现
/*每读出一个字符,将指针下移*/
int fputc(int c,FILE *fp);//函数
int puts(const char *s);
int fputs(const char* s, FILE *fp); //不添加\n
int getchar(void); //
int getc(FILE *fp); //宏实现
/*每读出一个字符,将指针下移*/
int fgetc(FILE *fp); //函数
char *gets(char *s); //不保留\n ,换成 \0
//读取 n-1个字符
char *fgets(char *s, int n, FILE *fp);
位置
返回位置
long
ftell(FILE *fp)
;
int
feof(FILE *fp)
; //非零代表读到了文件尾巴,或者出错
改变位置
void
rewind(FILE *fp)
;
//offset 偏移量
int
fseek(fp, long offset, origin)
;
SEEK_SET //开头
origin: SEEK_CUR //当前位置
SEEK_END //末尾
按数据块读写文件
size_t
fread
(void *buffer, size_t size, size_t count, FILE *stream);
size_t
fwrite
(void *buffer,size_t size, size_t count, FILE *stream);
int buffer[10];
fwrite(buffer,sizeof(int),10,fp)
;
fread(buffer, sizeof(int),10,fp)
;
文件缓冲
//输出缓冲
fflush(fp) //清洗输出流(写入输出设备)
//输入缓冲 :保存到缓冲区(内存上)
fclose(f);