文件io 之 标准io

这篇文章介绍了C语言中的标准IO和系统IO的区别,标准IO具有缓存区,而系统IO直接操作文件。文中提供了一个使用标准IO进行文件读写,包括添加和查询人员信息的示例程序。程序涉及的关键函数有fopen,fclose,fgets,fputs等。
摘要由CSDN通过智能技术生成

标准io是C语言标准库提供的

系统io是由系统提供的

标准io : 打开和关闭 : fopen fclose

对于(若干)数据进行操作: fwrite fread

对于数据进行操作: fgets fputs

对于字符进行操作   fgetc fputc

特殊:

feof判断是否读到末尾

ferror 判断读取是否成功

 

 

 

重点:系统io和标准io 的区别

系统io更偏向于操作系统的文件,标准io是对文件进行操作,系统io没有缓存区,标准io有缓存区

 

标准io

系统io 

 

 一篇基于标准io的文件信息人名的添加

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

#define INQUIRY_INFORMATION 1
#define ADD_PERSON_INFORMATION 2
#define OPENED_FILES "./a1.txt"
#define NUMBER_OF_BYTES_STORED_IN_A_ROW 20

typedef struct documents
{
    FILE *OBJ_FP;

} DC, *DMC;

int QueryInformationFunctionModule(DMC bip)
{
    printf("查询人员信息如下:\n");
    char storage[NUMBER_OF_BYTES_STORED_IN_A_ROW];
    while (1)
    {
        memset(storage, 0, NUMBER_OF_BYTES_STORED_IN_A_ROW);
        if (fgets(storage, NUMBER_OF_BYTES_STORED_IN_A_ROW, bip->OBJ_FP) == NULL)
        {
            if (ferror(bip->OBJ_FP))
            {
                perror("不好意思获取真的失败了哦");
                return -1;
            }
            if (feof(bip->OBJ_FP))
            {
                printf("读取完成了呢\n");
                break;
            }
        }
        printf("名字:%s", storage);
    }

    return 0;
}

int AddInformationFunctionModule(DMC bip)
{
    char storage[NUMBER_OF_BYTES_STORED_IN_A_ROW];
    printf("现在正在进行人员信息的录入,请输入要添加人的姓名:\n");
    scanf("%s", storage);
    if (fputs(storage, bip->OBJ_FP) == EOF)
    {
        printf("文件写入失败喽\n");
        return -1;
    }
    fputs("\n", bip->OBJ_FP);
    return 0;
}

int ReceivingData(DMC bip)
{
    int functionSelection;
    while (1)
    {
        printf("请选择你要进行的功能,1,查询所有人员信息,2,添加新成员信息:\n");
        scanf("%d", &functionSelection);
        if (functionSelection == INQUIRY_INFORMATION)
        {
            if (QueryInformationFunctionModule(bip) == -1)
            {
                perror("查询失败了\n");
                return -1;
            }
            break;
        }
        else if (functionSelection == ADD_PERSON_INFORMATION)
        {
            if (AddInformationFunctionModule(bip) == -1)
            {
                perror("失败喽");
                return -1;
            }
            break;
        }
        else
        {
            printf("指令有误请重新选择");
            system("clear");
        }
    }

    fclose(bip->OBJ_FP);
}

int FunctionFunction()
{
    DMC bip = (DMC)malloc(sizeof(DC));
    bip->OBJ_FP = fopen(OPENED_FILES, "a+");
    if (bip->OBJ_FP == NULL)
    {
        perror("打开文件失败喽");
        return -1;
    }
    ReceivingData(bip);
}

int main(int argc, char const *argv[])
{
    if (FunctionFunction() == -1)
    {
        perror("功能有误,再见!");
        return -1;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值