FILE 对象包含信息
文件描述符
该流的缓冲区
缓冲区长度
当前缓冲区的字节数
出错标志
#include <wchar.h> 改变流的定向
int fwide(FILE *stream, int mode);
mode:
mode>0 宽字节流、
mode<0 字节流
返回值:
> 0 宽字节流、
< 0 字节流
全缓冲:缓冲区满时执行I/O操作
行缓冲:遇见换行符时执行I/O操作
不带缓冲:立即执行I/O操作
更改缓冲类型函数:
#include <stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
mode:
_IONBF unbuffered 不带缓冲
_IOLBF line buffered 行缓冲
_IOFBF fully buffered 全缓冲
size(buf) ==0 <=>不带缓冲
强制冲刷一个流:
#include <stdio.h>
int fflush(FILE *stream);
文件的打开:
#include <stdio.h>
从指定路径打开一个文件:
FILE *fopen(const char *path, const char *mode);
指定一个文件打开一个预定义的流:
FILE *fdopen(int fd, const char *mode);
从已有的文件描述符并使一个标准的I/O流该描述符相结合;通常用于创建管道和socket文件描述符:
FILE *freopen(const char *path, const char *mode, FILE *stream);
mode:
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 begin‐
ning 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.
文件的关闭:
#include <stdio.h>
int fclose(FILE *fp);
一次读取一个字符:
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
可以将读取的数据据再次压入到流中:int ungetc(int c, FILE *stream);
单字符输出函数:
#include <stdio.h>
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
输入一行:
int gets(char *buf); 从标准输入流读
int fgets(char *buf,int len ,FILE * fp);
输出一行:
int puts(const char *s); 把一个以null结尾的字符串输出到标准输出;
int fputs(const char *s, FILE *stream); 把一个以null结尾的字符串输出到指定的流,null终止符不输出;
二进制I/O操作:
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
流定位:
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream); 定位到文件的开始位置:
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, const fpos_t *pos);
whence:
SEEK_SET
SEEK_CUR
SEEK_END
格式话输出:
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
格式化输入:
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
文件描述符
该流的缓冲区
缓冲区长度
当前缓冲区的字节数
出错标志
#include <wchar.h> 改变流的定向
int fwide(FILE *stream, int mode);
mode:
mode>0 宽字节流、
mode<0 字节流
返回值:
> 0 宽字节流、
< 0 字节流
全缓冲:缓冲区满时执行I/O操作
行缓冲:遇见换行符时执行I/O操作
不带缓冲:立即执行I/O操作
更改缓冲类型函数:
#include <stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
mode:
_IONBF unbuffered 不带缓冲
_IOLBF line buffered 行缓冲
_IOFBF fully buffered 全缓冲
size(buf) ==0 <=>不带缓冲
强制冲刷一个流:
#include <stdio.h>
int fflush(FILE *stream);
文件的打开:
#include <stdio.h>
从指定路径打开一个文件:
FILE *fopen(const char *path, const char *mode);
指定一个文件打开一个预定义的流:
FILE *fdopen(int fd, const char *mode);
从已有的文件描述符并使一个标准的I/O流该描述符相结合;通常用于创建管道和socket文件描述符:
FILE *freopen(const char *path, const char *mode, FILE *stream);
mode:
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 begin‐
ning 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.
文件的关闭:
#include <stdio.h>
int fclose(FILE *fp);
一次读取一个字符:
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
可以将读取的数据据再次压入到流中:int ungetc(int c, FILE *stream);
单字符输出函数:
#include <stdio.h>
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
输入一行:
int gets(char *buf); 从标准输入流读
int fgets(char *buf,int len ,FILE * fp);
输出一行:
int puts(const char *s); 把一个以null结尾的字符串输出到标准输出;
int fputs(const char *s, FILE *stream); 把一个以null结尾的字符串输出到指定的流,null终止符不输出;
二进制I/O操作:
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
流定位:
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream); 定位到文件的开始位置:
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, const fpos_t *pos);
whence:
SEEK_SET
SEEK_CUR
SEEK_END
格式话输出:
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
格式化输入:
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
#include<stdio.h>
void stdfileTest()
{
FILE *fd = fopen("b.txt","a+");
if(fd == NULL )
cout<<"oepn fail"<<endl;
char buf[5]={'d','e','f','g','h'};
for(int i=0;i<5;i+=2)
{
fputc(buf[i],fd);
putchar(buf[i+1]);
}
fflush(fd);
int tmp =0;
//if(ungetwc(101,fd)==EOF)
cout<<"back fail"<<endl;
fseek(fd,SEEK_SET,0);
while( (tmp = fgetc(fd))!=EOF)
{
//cout<<"tem= "<<tmp<<endl;
// if(tmp == 98 )ungetwc(tmp,fd);
printf("tem = %d :%c\n",tmp,tmp);
}
fclose(fd);
cout<<"====== ======"<<endl;
FILE *fd1 = fopen("c.txt","a+");
char buffer[1024];
setbuffer(fd1,buffer,1024);
if(fd1 ==NULL)
{
cout<<"open fail"<<endl;
}
fputs("test fputs\n",fd1);
// sleep(20);
//fseek(fd1,SEEK_SET,0);
rewind(fd);
char buf1[128] = {0};
fgets(buf1,128,fd1);
cout<<"buf = "<<buf1<<endl;
fclose(fd1);
}
int main()
{
cout<<"=====stdfilefwriteTest======"<<endl;
stdfileTest();
return 1;
}