要变优秀的科啊啊啊啊啊啊!!!

要变优秀的科啊啊啊啊啊啊!!!

IO输入输出,操作对象是文件

 

标准IO:
        getchar putchar scanf printf gets puts  -> 标准IO

        #include <stdio.h>      

        fopen/fclose 
        fgetc/fputc 
        fgets/fputs
        fscanf/fprintf
        fread/fwrite 
        fseek/rewind/ftell 

从文件中读写数据的流程:
        打开文件 -> 读写文件 -> 关闭文件

      fopen                   fclose
                    fgetc/fputc     单个字符的读写
                    fgets/fputs     字符串的读写
                    fscanf/fprintf  格式化字符串的读写
                    fread/fwrite    二进制文件的读写

  7.函数接口:
        1.fopen
          FILE *fopen(const char *pathname, const char *mode);
          功能:
            打开pathname对应的文件并与其建立一个文件流
          参数:
            pathname:要打开文件路径的字符串
            mode:
                r       只读            文件不存在报错,文件存在只读打开
                r+      读写            文件不存在报错,文件存在读写打开
                w       只写            文件不存在创建,文件存在将文件内容清空,只写打开
                w+      写读            文件不存在创建,文件存在将文件内容清空,写读打开
                a       追加只写        文件不存在创建,文件存在追加只写打开
                a+      追加写读        文件不存在创建,文件存在追加写读打开
          返回值:
              成功返回打开的文件流指针
              失败返回NULL

#include <stdio.h>

int main(void)
{
	FILE *fp = NULL;

	fp = fopen("a.txt", "r");
	if (NULL == fp)
	{
		perror("fail fopen");
		return -1;
	}

	printf("fopen success!\n");

	fclose(fp);

	return 0;
}

        2.fclose 
          int fclose(FILE *stream);
          功能:
            关闭文件,释放文件流指针
          参数:
            stream:文件流指针
          返回值:
            成功返回0 
            失败返回EOF(-1)

 8.文件流:
        1.具有方向性(读写)
        2.具有连续性
        3.具有顺序性

        句柄:操作对象的一个抽象

9.特殊的三个文件流:
        stdin   标准输入流          从终端读取数据
        stdout  标准输出流          向终端打印数据
        stderr  标准出错流          向终端打印数据 

        getchar、scanf、gets 通过stdin来读取终端数据
        putchar、printf、puts通过stdout来向终端输出数据
        perror通过stderr来向终端输出数据

#include <stdio.h>

int main(void)
{
	fclose(stdin);
	fclose(stdout);
	fclose(stderr);
	getchar();
	
	printf("hello world!\n");
	perror("hello world!\n");

	return 0;
}

 10.标准IO缓存:
        缓存分为3类:
            1.全缓存  4k
                缓存区满刷新
                
                刷新条件:
                    1.缓存区存满刷新(全缓存大小:4096)
                    2.fflush函数强制刷新
                    3.程序结束/fclose刷新

                与文件建立的缓存

            2.行缓存  1k
                遇到\n刷新  

                刷新条件:
                    1.缓存区存满刷新(行缓存大小:1024)
                    2.遇到\n刷新 
                    3.fflush函数强制刷新
                    4.程序结束/fclose刷新

                与终端建立的缓存            stdin   stdout 

            3.不缓存
                直接刷新

                缓存区大小 0k               stderr

                人机交互、界面控制、出错处理

            4.setvbuf
              int setvbuf(FILE *stream, char *buf, int mode, size_t size);
              功能:
                改变一个流的缓存类型
              参数:
                stream:文件流指针
                buf:指定缓存空间的首地址
                mode:
                    _IONBF  不缓存
                    _IOLBF  行缓存
                    _IOFBF  全缓存
                size:
                    设定缓存区的大小
              返回值:
                成功返回0 
                失败返回非0 

#include <stdio.h>

int main(void)
{
	char tmpbuff[4096] = {0};

//	setvbuf(stdout, NULL, _IONBF, 0);		//设置成不缓存
//	setvbuf(stdout, tmpbuff, _IOFBF, 4096);	//设置成全缓存
	setvbuf(stdout, tmpbuff, _IOLBF, 1024);	//设置成行缓存

	printf("hello world\n");
	
	while (1)
	{
		
	}

	return 0;
}

 

02-18 21:54