标准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;
}