求助 C语言中如何往txt文件增加数据

leoJD leoJD 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
int size=0;
struct GZ
{long int num;
char name[20];
float jbgz;
float jt;
float ks;
float sfgz;
};

struct GZ s[100];

void Input()
{
FILE *fp;
int n=0;
fp=fopen("e:\\1.txt","r");
while(fscanf(fp,"%ld %s %f %f\n",&s[n].num,s[n].name,&s[n].jbgz,&s[n].jt)!=EOF)
{
n++;
}
fclose(fp);
size=n;
}

void Disp()
{
int n;

printf("\t\t\tNum Name jbgz jt ks sfgz \n");
for(n=0;n<size;n++)
{
printf("\t\t\t%-5ld%-10s%-7.2f%-7.2f%-7.2f%-7.2f\n",s[n].num,s[n].name,s[n].jbgz,s[n].jt,s[n].ks,s[n].sfgz);
printf("\n");
}
}


void Count()/*计算实发工资*/
{
float c,k,a,b;
int n;
for(n=0;n<size;n++)
{
a=s[n].jbgz ;
b=s[n].jt ;
c=a+b;
if(c<=1500) k=0;
else if(c<=2000) k=(float)((c -1500)*0.05);
else if(c>2000) k=(float)((c -1500)*0.05+(c -2000)*0.1);
s[n].sfgz =c-k ;
s[n].ks =k;
printf("\t\t\t%-5ld %-10s %-7.2f %-7.2f %-7.2f %-7.2f\n",s[n].num,s[n].name,s[n].jbgz,s[n].jt,s[n].ks,s[n].sfgz);
printf("\n");
}
}


void Append()/*追加1条职工信息*/
{
int a;
FILE *fp;
printf("please input the people's information that you want to append.\n");
a=size;
fp=fopen("e:\\1.txt","a");
printf("\t\t\tplease re-input the Num Name jbgz jt.\n");
scanf("%ld %s %f %f",&s[a].num,s[a].name,&s[a].jbgz,&s[a].jt);

fwrite(&s[a],sizeof(GZ),1,fp);
fclose(fp);

}


void Modify()/*修改数据*/
{
int a,b,i;
FILE *fp;
printf("please input the people's id number that you want to change.\n");
scanf("%d",&a);
a=a-201700;
if(a>size) printf("sorry the people not exist.\n");
else
{
b=a-1;
fp=fopen("e:\\1.txt","a");
printf("\t\t\tplease re-input the Num Name jbgz jt.\n");
scanf("%ld %s %f %f",&s[b].num,s[b].name,&s[b].jbgz,&s[b].jt);
fseek(fp,i*sizeof(GZ),2);
fwrite(&s[b],sizeof(GZ),1,fp);
fclose(fp);
}

}

void Query()/*查询职工信息*/
{
int a;
printf("please input the people's information that you want to change.\n");
scanf("%d",&a);
a=a-201700;
if(a>size) printf("sorry the people not exist.\n");
else
{
FILE *fp;
float c,k,h,b;
int m=0,n;
printf("\t\t\tNum Name jbgz jt ks sfgz \n");
for(n=0;n<size;n++)
{
h=s[n].jbgz ;
b=s[n].jt ;
c=h+b;
if(c<=1500) k=0;
else if(c<=2000) k=(float)((c -1500)*0.05);
else if(c>2000) k=(float)((c -1500)*0.05+(c -2000)*0.1);
s[n].sfgz =c-k ;
s[n].ks =k;
}
printf("\t\t\t%-5ld %-10s %-7.2f %-7.2f %-7.2f %-7.2f\n",s[a].num,s[a].name,s[a].jbgz,s[a].jt,s[a].ks,s[a].sfgz);
printf("\n");
}
}


void main()
{
int choice;
menu:printf("\t\t\t1.Input employee's information\n");
printf("\t\t\t2.Display employee's information\n");
printf("\t\t\t3.Count\n");
printf("\t\t\t4.Append a employee information\n");
printf("\t\t\t5.Query a employee information\n");
printf("\t\t\t6.Modify employee's intformation\n");
printf("\t\t\t0.Exit system\n\n");
printf("\t\t\tPlease enter choice(0~7)");
scanf("%d",&choice);
switch(choice)
{
case 1:Input();break;
case 2:Disp();break;
case 3:Count();break;
case 4:Append();break;
case 5:Query();break;
case 6:Modify();break;
case 0:exit(0);
}
if(choice<0||choice>6)printf("\n\n\n\t\tplease choose again!\n\n");
goto menu;
}

请问这中间的modify和append应如何编写,谢谢
...全文

相关推荐

c语言读取txt文件内容简单实例

在本篇文章里小编给大家整理的是关于c语言如何读取txt文件内容,需要的朋友们可以参考下。

weixin_38555350

json2txt:将压缩的JSON文件转换为TXT格式

JavaScript Object Notation(JSON)是一种轻量级的数据交换格式,它基于JavaScript的一个子集。由于其简洁性和易于阅读性,JSON被广泛用于Web应用和服务之间的数据交换。它以纯文本形式存储,易于人阅读和编写,同时也易于机器解析和生成。JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,因其结构简单、易于阅读和编写而广泛应用于网络数据交换。然而,在某些场景下,尤其是数据存储和日志记录时,纯文本格式(TXT)更为通用。

安检

C语言从txt文件中逐行读入数据存到数组中的实现方法

下面小编就为大家带来一篇C语言从txt文件中逐行读入数据存到数组中的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38673694

c语言读取txt文件数据

在C语言中,读取txt文件是一项非常基础的操作,也是我们日常编程中经常会用到的技能。本文将详细介绍C语言如何读取txt文件,并提供代码实现。

chuxuezhe_987

c语言从文件中读取数据,对数据进行冒泡,选择,插入三种排序

首先要使用c语言必须要有特定的语言来打开文本文件 “r”:只能从文件中读数据,该文件必须先存在,否则打开失败 “w”:只能向文件写数据,若指定的文件不存在则创建它,如果存在则先删除它再重建一个新文件 “a”:向文件增加新数据(不删除原有数据),若文件不存在则打开失败,打开时位置指针移到文件末尾 “r+”:可读/写数据,该文件必须先存在,否则打开失败 “w+”:可读/写数据,用该模式打开新建一个文件...

kirk2508
评论 10
收藏 已收藏
分享
返回顶部
leoJD
C语言 70,040 成员 243,237 内容