文件的操作(2)----文件读写函数

1.顺序读写函数介绍

1.1字符,文本行输入输出函数

1.1.1 fgetc

字符输入函数,一次读取一个字符,读取失败返回EOF。

int main(){
    FILE* pf = fopen("test.txt","r");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    int ch = fgetc(pf);
    printf("%c\n",ch);
    fclose(pf);
    pf = NULL;
}

1.2 fputc

字符输出函数,一次输出(写)一个字符,适用所有输出流。

int main(){
    FILE* pf = fopen("test.txt","W");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    fputc('x',pf);
    fputc('y',pf);
    
    fclose(pf);
    pf = NULL;
}

1.3 fputc,fgetc—文件的拷贝

把test.txt中的内容拷贝一份放到test2.txt中。

int main(){
    FILE* pf = fopen("test.txt","r");
    if(pf == NULL){
        perror("fopen");
        return 1;
    }
    FILE* pfw = fopen("text2.txt","w");
    if(pfw == NULL){
        perror("fopen-2");
        fclose(pf);
        pf = NULL;
        return 1;
    }

    int ch = 0;
    while((ch = fgetc(pf)!= EOF){
        fputc(ch,pf);
    }
    fclose(pf);
    pf = NULL;
    fclose(pfw);
    pfw = NULL;
    return 0;

}

1.4 fputs

将一行数据写入文件中,同样适用所有输出流。

int main(){
    FILE* pf = fopen("test.txt","W");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    fputs("hello,world",pf);    
    fclose(pf);
    pf = NULL;
}

1.5 fgets

fgets与fgetc略有不同,fgets的三个参数分别是放置字符串的字符串,最多拷贝多少字符,以及流,作用是读取文件中的一行数据,读取失败或遇到文件末尾会返回空指针。

int main(){
    FILE* pf = fopen("test.txt","r");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    char arr[20] = {0};
    fgets(arr,20,pf);
    
    fclose(pf);
    pf = NULL;
}

需要把读取的数据显示到屏幕上,使用printf函数即可。注:fgets中的第二个参数是包括字符串结束标志\0的。

1.2格式化以及二进制输入输出函数

1.2.1 fprintf

适用于所有输出流,参数依次是流指针,和格式。  额外的,sprintf函数是将格式化的数据变为字符串。

fprintf(pf,"%d %f %s",100,1.0f,"abcd");

1.2.2 fscanf

适用于所有输入流。

下面代码作用是把数据保存到结构体中,对应的,sscanf是从字符串中提取出已经格式化的数据。

struct S{
    int n;
    float f;
    char arr[20];
}

int main(){
    struct S s = {0};
    FILE* pf = fopen("test.txt","r");
    if(pf == NULL){
        perror("fopen");
        return 1;
    }
    fscanf(pf,"%d %f %s",&(s.n),&(s.f),s.arr);
    fclose(pf);
    pf = NULL;
}

1.2.3 fread

适用于文件流,二进制读。

int main(){
    int arr[] = {1,2,3,4,5,6};
    FILE* pf = fopen("test.txt","Wb");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    fread(arr,sizeof(int),6,pf);  
    fclose(pf);
    pf = NULL;
}

1.2.4 fwrite

适用于文件流,二进制写。

int main(){
    int arr[] = {1,2,3,4,5,6};
    FILE* pf = fopen("test.txt","Wb");
    if(pf ==NULL)
    {
        perror("fopen");
        return;
    }
    fwrite(arr,sizeof(int),6,pf);  
    fclose(pf);
    pf = NULL;
}

 2.随机读写函数介绍

2.1 fseek

函数声明:

int fseek(FILE* stream, long int offset, int origin);

流指针,偏移量以及初始位置。

origin可选有三个参数:SEEK_SET,SEET_END,SEEK_CUR,文件指针开始,结束,当前位置。

2.2 ftell

返回当前文件指针的偏移量。

2.3 rewind

让文件指针返回初始位置,消除偏移量。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值