IO进程

一:标准IO

1.1:文件类型

l(链接文件)s(套接字文件) p(管道文件) -(普通文件) b(块设备文件) c (字符设备文件) d(目录文件)

1.3:特点

1:有缓存区

2:围绕流进行操作,用FILE*来描述,FILE代表的是结构体,描述所操作文件的信息

3:默认流:stdin(标准输入) stdout(标准输出) stderr(标准出错)

1.4:查看FILE

补充:sudo ctags -R的使用(可以追代码):简历搜索索引

vi -t FILE

选择合适的编号

将光标定位在目标位置,ctrl+] :向下追代码

ctrl+t:回退

q:退出

二:缓冲区分类(应用层开辟)

1:行缓存:终端操作相关

刷新缓存条件:1:程序正常退出

                         2:\n刷新

                          3:缓存区满(1024)

#include<stdio.h>

int main(int argc, char const *argv[])
{
   for (int i = 0; i < 300; i++)
   {
    printf("%4d",i);
   }
   while(1);
}
刚满也不会输出,有溢出才会输出

第二种方法:

#include<stdio.h>
FILE;

int main(int argc, char const *argv[])
{
//    for (int i = 0; i < 300; i++)
//    {
//     printf("%4d",i);
//    }
//    while(1);
stdout;//标准输出
//先启动缓冲区,写入数据
printf("yue");
//通过标准输出拿到起始地址和结尾地址,求的缓冲区大小
printf("% ld\n",stdout->_IO_buf_end-stdout->_IO_buf_base);
}

4:强制刷新(fflush)

man手册查看

printf("yue");
fflush(NULL);//强制缓存区刷新
while (1);
}

2:全缓存:文件操作相关

刷新条件:1):程序正常退出

                    2):缓存区满刷新

                     3):强制刷新:fflush

3:不缓存:标准错误 stderr

三:函数

3.1:打开文件

FILE *fopen(const char *path, const char *mode);
参数:
    path:打开文件
    mode:打开方式
        r:只读,流被定位到文件开头
        r+:可读可写,流被定位到文件开头
        w:只写,文件不存在创建,文件存在清空,流被定位到文件开头
        w+:可读可写,文件不存在创建,文件存在清空,流被定位到文件开头
        a:追加,文件不存在创建存在追加,流被定位到文件末尾
        a+:可读可写,文件不存在创建,存在追加,开始进行读时从头读,进行写时流被定位到文件末尾
返回值:成功:文件流
       失败:NULL,并且设置errno(错误码)
#include<stdio.h>

int main(int argc, char const *argv[])
{
    FILE *p=fopen("./test.c","r");
    if(p==NULL)
    {
        printf("fopen file err\n");
        return -1;
    }
    printf("fopen file success");
    return 0;
}

2:关闭文件

int fclose(FILE* stream);
功能:关闭文件
参数:stream:文件流
#include<stdio.h>

int main(int argc, char const *argv[])
{
    FILE *p=fopen("./test.c","r");
    if(p==NULL)
    {
        printf("fopen file err\n");
        return -1;
    }
    printf("fopen file success\n");
    return 0;
    fclose(p);
}

3:读写文件

1:每次一个字符的读写(fgetc)
int fgetc(FILE *stream);
功能:从文件中读一个字符
参数:stream:文件流
返回值:成功:读到字符
       失败或读到文件末尾:EOF(-1)
#include <stdio.h>
int ferror(FILE *stream);
功能:判断读文件时是否出错
返回值:非0表示出错

int feof(FILE *stream);
功能:判断读文件时是否到文件末尾
返回值:非0表示读到文件末尾

2:fputc

#include <stdio.h>
int  fputs(const char *s,  FILE *stream);
功能:向文件中写字符串
参数:s:要写的内容
    stream:文件流
返回值:成功:非负整数
       失败:EOF

1.2:代码表示:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE *p = fopen("./test.txt", "w+");
    if (p == NULL)
    {
        printf("fopen file err\n");
        return -1;
    }
    printf("打开成功\n");

    fputc('h',p);
    fputc(65,p);
    rewind(p);//将文件位置指针定位到起始位置
    int ch = fgetc(p);
    if (ch == EOF)
    {
        perror("fgetc err");
        return -1;
    }
    printf("%c\n", ch);

    // 失败或者读到末尾
    if (ch == EOF)
    {
        if (ferror(p)) // 读文件时出错
        {
            perror("fgetc error");
            return -1;
        }
        if (feof(p)) // 读到末尾
        {
            perror("读到末尾");
            return -1;
        }
    }

    // 关闭文件
    fclose(p);
}
1.2: 每次一个字符串的读写

fgets:

#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从文件中读取一串字符
参数:s:存放读取的字符串的首地址
     size:读取的大小
     stream:文件流
返回值:成功:读取的字符串的首地址
      失败或读到文件末尾:NULL
      
特性:1.一次调用最多读取一行数据
     2.实际读到个数为size-1个,末尾自动添加\0

 fputs:

#include <stdio.h>
int  fputs(const char *s,  FILE *stream);
功能:向文件中写字符串
参数:s:要写的内容
    stream:文件流
返回值:成功:非负整数
       失败:EOF

#include<stdio.h>

int main()
{
FILE *p=fopen("text.txt","r");
int a[20];
if(p==NULL)
{
perror("err");
return -1;
}
else
printf("sucess");
fgets(a,sizeof(a),p);
for(int i=0;i<6;i++)
{
printf("%d ",a[i]);
}
fclose(p);
return 0;
}
 4:二进制读写文件

fread:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从文件流读取多个元素
参数:	ptr :用来存放读取元素 (可以用来读取任意类型的数据)
        size :元素大小  sizeof(数据类型)
		nmemb :读取对象的个数
		stream :要读取的文件
返回值:成功:读取对象的个数
      读到文件尾或失败:0

fwrite

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:按对象写
参数:同上	
返回值:成功:写的元素个数
      失败 :0
#include<stdio.h>
int main(int argc, char const *argv[])
{
    FILE *p=fopen("./test.txt","w+");
    if(p==NULL)
    {
        perror("打开失败\n");
        return -1;
    }
    int a[3]={1,2,3};
    int b[3]={0};
    //要写的数据地址,一个数据的大小,多少个数据,文件流
    fwrite(a,sizeof(int),3,p);
    rewind(p);//将文件的位置指针定位到开头位置
    fread(b,sizeof(int),3,p);
    for(int i=0;i<3;i++)
    {
        printf("%d \n",b[i]);
    }
    return 0;
}

四:文件的定位操作

1:rewind:将文件位置指针定位到起始位置

2:fseek:

int fseek(FILE *stream, long offset, int whence);
功能:文件的定位操作
参数:stream:文件流
     offset:偏移量:正数表示向后文件尾部偏移,负数表示向文件开头偏移
     whence:相对位置:
           SEEK_SET:相对于文件开头
           SEEK_CUR:相对于文件当前位置
           SEEK_END:相对于文件末尾
返回值:成功:0
        失败:-1

3:ftell

long ftell(FILE *stream);
功能:获取位置指针当前的文件位置
参数:要检测的文件流
返回值:成功:当前的文件位置,出错:-1

注释:rewind(p)与fseek(p,0,SEEK_SET)等价

           可以通过其计算文件中字符的个数

           但是当fopen打开方式为追加时,即a或者a+,fseek不能用于计算字符个数

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值