读取写入文件三种方法

按照字符读取和写入

#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\n");
        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\n");
        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 \n");
        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\n");
        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\n");
        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\n",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;
}

 

猜你喜欢

转载自www.cnblogs.com/wanghao-boke/p/11831967.html