zoukankan      html  css  js  c++  java
  • 读取写入文件三种方法

    按照字符读取和写入

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int fputc_func(char *filename)
    {
        int i = 0;
        FILE *fp = NULL;
        char buf[64] = "this is function that writting file by char";
    
        fp = fopen(filename,"w+");
        if (NULL == fp)
        {
            printf("func open file error
    ");
            return -1;
        }
    
        int len = strlen(buf);
        for (i = 0; i < len; i++)
        {
            fputc(buf[i],fp);
        }
    
        fclose(fp);
    
        return 0;
    }
    
    int    fgetc_func(char *filename)
    {
        int i = 0;
        FILE *fp = NULL;
        
        char buf[64];
        
        fp = fopen(filename, "r+");
        if (NULL == fp)
        {
            printf("func fopen() error
    ");
            return -1;
        }
    
        while (!feof(fp))
        {
            char temp = fgetc(fp);
            printf("%c",temp);
        }
    
        if (fp != NULL)
        {
            fclose(fp);
        }
    
        return 0;
    }
    
    int main()
    {
        char *filename = "char.txt";
        fputc_func(filename);
        fgetc_func(filename);
        return 0;
    }

    按照行读取写入

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int fputs_func()
    {
        int i = 0;
        FILE *fp = NULL;
    
        char *filename = "./char.txt";
    
        char buf[32] = "abcdefghijklmn";
    
        fp = fopen(filename,"w+"); //open file with both read and write
                                    //if the file not exit, then create
        if (NULL == fp)
        {
            printf("func_puts fopen error 
    ");
            return -1;
        }
    
        fputs(buf, fp);
        fclose(fp);
    
        return 0;
        
    }
    
    int fget_func()
    {
        int i = 0;
        FILE *fp = NULL;
    
        char *filename = "char.txt";
        char buf[1024];
        
        /*
         open file with both read and write,
         if the file not is exist, then return a error
         */
        fp = fopen(filename, "r+");
        if (NULL == fp)
        {
            printf("func_fget fopen() error");
            return -1;
        }
    
        while (!feof(fp))
        {
            char *p = fgets(buf,1024,fp);
            if (p == NULL)
            {
                break;
            }
            printf("%s",buf);
        }
        
        if (fp != NULL)
        {
            fclose(fp);
        }
    
        return 0;
    }
    
    
    int main()
    {
        fputs_func();
        fget_func();
        return 0;
    }

    按照块读取写入

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct Teacher
    {
        char name[64];
        int age;
    }Teacher;
    
    int fwrite_func(char *filename)
    {
        int i = 0;
        FILE *fp = NULL;
    
        Teacher tArray[3];
        int myN = 0;
    
        for (i = 0 ; i < 3; i++)
        {
            sprintf(tArray[i].name, "%d%d%d", i+1, i+1, i+1);
            tArray[i].age = i + 31;
        }
    
        fp = fopen(filename, "wb"); //write file with binary
        if (fp == NULL)
        {
            printf("create file error
    ");
            return;
        }
    
        for (i = 0; i < 3; i++)
        {
            myN = fwrite(&tArray[i], sizeof(Teacher), 1, fp);
        }
    
        if (fp != NULL)
        {
            fclose(fp);
        }
    
        return 0;
    }
    
    int fread_func(char *filename)
    {
        int i = 0;
        FILE *fp = NULL;
        int myN = 0;
    
        Teacher tArray[3];
    
        fp = fopen(filename, "r+b");
        if (fp == NULL)
        {
            printf("create file error
    ");
            return -1;
        }
        
        for (i = 0; i < 3; i++)
        {
            myN = fread(&tArray[i], sizeof(Teacher), 1, fp);
        }
    
        for (i = 0; i < 3; i++)
        {
            printf("name:%s, age:%d
    ",tArray[i].name,tArray[i].age);
        }
    
        if (fp != NULL)
        {
            fclose(fp);
        }
    
        return 0;
    }
    
    int main()
    {
        char *filename = "bolck.txt";
        fwrite_func(filename);
        fread_func(filename);
        return 0;
    }
  • 相关阅读:
    ipa在线下载安装(itms-services)
    linux环境下无文件执行elf
    Linux Running State Process ".so"、"code" Injection Technology
    VS2013本地C++单元测试框架
    vs的环境变量
    利用rundll32执行程序的函数执行程序
    动态so注入
    ELF运行时注入
    MailKit系列之转发电子邮件
    WPF实战之一 桌面消息框(右下角消息弹出框)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11831967.html
Copyright © 2011-2022 走看看