zoukankan      html  css  js  c++  java
  • 文件操作(二进制读写)

    /***
    a.txt
    ***/
    this is a word
    helloworld
    
    /***
    file.c
    ***/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        FILE *p = fopen("./a.txt","r");
        while(!feof(p))
        {
            char buf[100] = {0};
            fscanf(p,"%s",buf);
            printf("%s
    ",buf);
        }
        fclose(p);
        return 0;
    }

    fscanf()函数和scanf函数用法一样。fscanf是从一个文件中读取字符串,scanf是从键盘读取字符串。(遇到空格就停止)

    /***
    a.txt
    ***/
    23 + 45 = 
    35 + 63 =
    34 + 45 =
    
    /***
    file1.txt
    ***/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        int a,b;
        FILE *p = fopen("./a.txt","r");
        while(!feof(p))
        {
            char buf[100] = {0};
            //fgets(buf,"%s",p);
            //fscanf(p,"%s",buf);
            fscanf(p,"%d + %d = ",&a,&b);
            printf("a = %d , b = %d
    ",a,b);
            //printf("%s
    ",buf);
        }
        fclose(p);
        return 0;
    }
    
    /***
    fprintf.c
    ***/
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        FILE *p = fopen("./b.txt","w");
        char buf[100] = "hello world";
        fprintf(p,"%s",buf);
        fclose(p);
        return 0;
    } 

    fread()函数和fwrite()函数:操作文本文件和二进制文件

    fopen()函数只能读文本文件

    /***
    fread.c
    ***/
    
    #include<stdio.h>
    int main()
    {
        FILE *p = fopen("./a.txt","rb");
        char buf[100] = {0};
    fread(buf,sizeof(char),1,p); //第一个参数是缓冲区,第二个参数是读取字节大小,第三个 //参数表示读取字节个数,第四个参数表示文件指针
        printf("buf = %s
    ",buf);
        fclose(p);
        return 0;
    }
    
    /***
    fread1.c
    ***/
    
    #include<stdio.h>
    int main()
    {
        FILE *p = fopen("./a.txt","rb");
    
        while(!feof(p))
        {
            char buf[20] = {0};
            fread(buf,sizeof(char),sizeof(buf),p);
            printf("buf = %s
    ",buf);
        }
        fclose(p);
        return 0;
    }

    fread()函数有返回值,返回的是size_t类型,unsigned int ==》 %u

    /***
    fread.c
    ***/
    #include<stdio.h>
    int main()
    {
        FILE *p = fopen("./c.txt","rb");
        char buf[1024] = {0};
        size_t res = fread(buf,sizeof(char),sizeof(buf),p);
        printf("res = %u
    ",res);
    /*
        while(!feof(p))
        {
            char buf[20] = {0};
            fread(buf,sizeof(char),sizeof(buf),p);
            printf("buf = %s
    ",buf);
        }
    */
        fclose(p);
        return 0;
    }
    
    /***
    c.txt
    ***/
    asd

    运行结果:res = 4(文件结尾有一个换行字符‘ ’?);

    size_t res = fread(buf,sizeof(int),sizeof(buf),p);

    运行结果:res = 1;

    以上程序的运行结果和windos环境下读文本文件的结果会出现差异。

    /***
    fwrite.c
    ***/
    #include<stdio.h>
    
    int main()
    {
        FILE *p = fopen("./d.txt","wb");
        char buf[1024] = {0};
        buf[0] = 'a';
        buf[1] = 'b';
        buf[2] = 'c';
        fwrite(buf,sizeof(char),sizeof(buf),p);
        fclose(p);
        return 0;
    }

    运行结果:abc

    文件大小是1K(buf)数组大小,文件后面全是0。

    /***
    fwrite.c
    ***/
    #include<stdio.h>
    
    int main()
    {
        FILE *p = fopen("./d.txt","w");
        int a = 0x12345678;
        fwrite(&a,sizeof(char),4,p);
        fclose(p);
        return 0;
    }
  • 相关阅读:
    MVC身份验证及权限管理
    EasyPR--开发详解
    ASP.NET 安全认证
    将Excel导入到数据中
    ExtJS 4 树
    ExtJS 4 表单
    ExtJS 4 Grids 详解
    ExtJS 4 类系统
    第4章 类型基础 -- 4.1 所有类型都从System.Object派生
    随滚动条浮动的链接块层
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11222797.html
Copyright © 2011-2022 走看看