c语言覆盖删除法,C语言中用文件覆盖法删除文件中数据源代码

匿名用户

1级

2009-07-08 回答

这样应该可以:

#include

#include

#include

#include

struct works_list

{

char name[10];

char addr[20];

char age[10];

char phone[13];

char sex[10];

char gongzi[10];

char xueli[10];

char zhiwu[10];

char xuhao[10];

char beizhu[50];

};

void menu()

{

}

/*接收多余的按键*/

void eatenter()

{

while(getchar() != '\n');

}

/*添加函数*/

void new_work()

{

FILE *da;

char choice;

struct works_list f;

if((da = fopen("data.txt", "a+")) == NULL)

{

printf("\n不能建立数据文件!!!");

exit(0);

}

do

{

printf("请输入序号:\n");

scanf("%s",f.xuhao );

eatenter();

printf("请输入姓名:\n");

scanf("%s",f.name );

eatenter();

printf("请输入住址:\n");

scanf("%s",f.addr);

eatenter();

printf("请输入年龄:\n");

scanf("%s",f.age);

eatenter();;

printf("请输入性别[m为女f为男]:\n");

scanf("%s",f.sex);

eatenter();

printf("请输入工资:\n");

scanf("%s",f.gongzi);

eatenter();

printf("请输入学历:\n");

scanf("%s",f.xueli);

eatenter();

printf("请输入职务:\n");

scanf("%s",f.zhiwu);

eatenter();

printf("请输入电话号码:\n");

scanf("%s",f.phone);

eatenter();

printf("\n\n\n输入完毕,是否保存?\n1.是,我要保存!2.不,我要重新输入!\n3.返回主菜单\n");

scanf("%s",&choice);

eatenter();

if('1'==choice)

{

fprintf(da, "%s %s %s %s %s %s %s %s %s\n",f.xuhao,f.name,f.addr,f.age,f.sex,f.gongzi,f.xueli,f.zhiwu,f.phone);

printf("保存完毕!\n");

system("cls");

menu();

}

else if('2'==choice)

{

system("cls");

}

else if('3'==choice)

{

system("cls");

menu();

}

else

{

system("cls");

printf("\n\n\n\n****************输入错误,系统返回至主菜单************\n\n\n\n");

menu();

}

if ('2' != choice)

fclose(da);

}while(choice==2);

}

void remove()

{

FILE *infile, *outfile;

struct works_list f;

char name[10];

infile = fopen("data.txt", "r");

outfile = fopen("temp123.txt", "w");

if (infile == NULL || outfile == NULL)

{

printf("\n打开文件出错!!!");

exit(0);

}

printf("请输入一个姓名:");

scanf("%s", name);

eatenter();

while (1)

{

memset(&f, 0, sizeof(struct works_list));

fscanf(infile, "%s %s %s %s %s %s %s %s %s",f.xuhao,f.name,f.addr,f.age,f.sex,f.gongzi,f.xueli,f.zhiwu,f.phone);

if (f.name[0] == 0)

break;

if (strcmp(name, f.name))

{

fprintf(outfile, "%s %s %s %s %s %s %s %s %s\n",f.xuhao,f.name,f.addr,f.age,f.sex,f.gongzi,f.xueli,f.zhiwu,f.phone);

}

}

fclose(infile);

fclose(outfile);

unlink("data.txt");

rename("temp123.txt", "data.txt");

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Keil使用C语言编程解析.gc文件的G命令代码,您可以按照以下步骤进行操作: 1. 打开Keil IDE,并创建一个新的C语言项目。 2. 将.gc文件复制到项目的源代码文件。 3. 在项目创建一个C源代码文件,例如`parse.c`,并在其编写解析函数。以下是一个示例代码,演示了如何解析.gc文件的G命令代码: ```c #include <stdio.h> #include <string.h> void parseGCommand(const char* gcFileName) { FILE* gcFile = fopen(gcFileName, "r"); if (gcFile == NULL) { printf("无打开.gc文件\n"); return; } char line[256]; while (fgets(line, sizeof(line), gcFile)) { // 检查是否为G命令 if (line[0] == 'G') { // 解析G命令的指令和参数 char command; int value; sscanf(line, "%c%d", &command, &value); // 处理解析结果 printf("解析到G命令:指令 = %c, 参数 = %d\n", command, value); // 在这里可以根据需要执行相关操作 } } fclose(gcFile); } int main() { const char* gcFileName = "your_file.gc"; parseGCommand(gcFileName); return 0; } ``` 请将代码的 "your_file.gc" 替换为您要处理的实际.gc文件的路径。 在示例代码,我们使用 `fopen` 函数打开.gc文件,并使用 `fgets` 函数逐行读取文件内容。然后,我们检查每行是否以 'G' 开头,以确定是否为G命令。如果是G命令,我们使用 `sscanf` 函数解析指令和参数的值。最后,我们在控制台打印解析结果,并可以根据需要执行相关操作。 请注意,示例代码仅演示了解析.gc文件的G命令的基本过程。具体的实现方可能因您的需求和.gc文件的格式而有所不同。您可能需要根据实际情况进行调整和扩展代码,以处理其他指令和参数。 重要提示:在解析任何文件之前,请确保您已经了解文件格式和规范,并进行相应的错误处理和容错机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值