标准C库对文件操作的引入 (二)((一)的补充+fput、fget、feof)

对上一节做的补充

f_seriesnewagain9.c 
#include <stdio.h>
#include <string.h>

int main()
{
//      FILE *fopen(const char *pathname, const char *mode);

        FILE *fp;
        char *str = "ganboss is very handsome";
        char readBuf[128] = {0};

        fp = fopen("./gannew.txt","w+");

//      size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

        // ptr buf
        // size sizeof(char)
        //geshu
        //which file
        int nwrite = fwrite(str,sizeof(char),strlen(str),fp);
//      fwrite(str,sizeof(char)*strlen(str),1,fp);

//      int fseek(FILE *stream, long offset, int whence);
        fseek(fp,0,SEEK_SET);

//      size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
        int nread = fread(readBuf,sizeof(char),strlen(str),fp);

        printf("read data: %s\n",readBuf);
        printf("read = %d,write = %d\n",nwrite,nread);

        return 0;
}

增加了 这个

printf("read = %d,write = %d\n",nwrite,nread);

运行结果为: 

对比 代码

#include <stdio.h>
#include <string.h>

int main()
{
//      FILE *fopen(const char *pathname, const char *mode);

        FILE *fp;
        char *str = "ganboss is very handsome";
        char readBuf[128] = {0};

        fp = fopen("./gannew.txt","w+");

//      size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

        // ptr buf
        // size sizeof(char)
        //geshu
        //which file
        int nwrite = fwrite(str,sizeof(char)*strlen(str),1,fp);
//      fwrite(str,sizeof(char)*strlen(str),1,fp);

//      int fseek(FILE *stream, long offset, int whence);
        fseek(fp,0,SEEK_SET);

//      size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
        int nread = fread(readBuf,sizeof(char)*strlen(str),1,fp);

        printf("read data: %s\n",readBuf);
        printf("read = %d,write = %d\n",nwrite,nread);

        return 0;
}
int nwrite = fwrite(str,sizeof(char),strlen(str),fp);
int nread = fread(readBuf,sizeof(char),strlen(str),fp);


int nwrite = fwrite(str,sizeof(char)*strlen(str),1,fp);
int nread = fread(readBuf,sizeof(char)*strlen(str),1,fp);

区别这两段的代码  分别是上下两个结果 

运行结果为:

当 fwrite 和 fread 的 size_t nmemb    为多少 打印出来就是多少

 

标准c库写入结构体到文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

struct Test
{
        int a;
        char c;

};
int main()
{
        FILE *fp;


        struct Test data1 = {100,'a'};
        struct Test data2;


        fp = fopen("./file1","w+");

        int n_write = fwrite(&data1,sizeof(struct Test),1,fp);

        fseek(fp,0,SEEK_SET);

        int n_read = fread(&data2,sizeof(struct Test),1,fp);

        printf("read : %d,%c\n",data2.a,data2.c);

        fclose(fp);



        return 0;

}

运行结果为 : 

 

fput、fget、feof

otherf_series11.c    //fput
#include <stdio.h>

int main()
{
        FILE *fp;
        fp = fopen("./ganboss.txt","w+");
        fputc('a',fp);
        fclose(fp);
        return 0;

}
otherf_series12.c  //fput
#include <stdio.h>
#include <string.h>

int main()
{
        FILE *fp;
        int i;
        char *str = "ganboss hen shuai yo!";
        int len = strlen(str);
        fp = fopen("./ganboss.txt","w+");
        for(i=0;i<len;i++){

                fputc(*str,fp);
                str++;
        }
        fclose(fp);
        return 0;

}
otherf_series13.c  //fget、feof
#include <stdio.h>
#include <string.h>

int main()
{
        FILE *fp;
        int c;
        fp = fopen("./ganboss.txt","r");

        while(!feof(fp))  //nonezero if reach end of file
        {
                c = fgetc(fp);
                printf("%c",c);

        }
        fclose(fp);
        return 0;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点灯小哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值