gpt4 book ai didi

c - 如何在C编程中删除记录中的特定行(无法进行删除)

转载 作者:行者123 更新时间:2023-11-30 20:27:49 25 4
gpt4 key购买 nike

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

struct person{
char stdNumb [20];
char firstName [20],lastName [20], icPass [20], nationality [20], gender [20], dateOfBirth [20];
char contact [20], address [30];
};

void appendData(){
FILE *fp;
struct person obj;
fp=fopen("D:\\data.txt","a");
printf("\n============================");
printf("\n ADD");
printf("\n============================\n\n");

printf("Student Number: ");
scanf("%s",&obj.stdNumb);

printf("First Name: ");
scanf(" %[^\n]s",obj.firstName);

printf("Last Name: ");
scanf(" %[^\n]s",&obj.lastName);

printf("IC/ Passport: ");
scanf("%s",&obj.icPass);

printf("Nationality: ");
scanf("%s",obj.nationality);

printf("Gender: ");
scanf("%s",&obj.gender);

printf("Date of Birth: ");
scanf("%s",obj.dateOfBirth);

printf("Contact: ");
scanf("%s", &obj.contact);

printf("Address: ");
scanf(" %[^\n]s", &obj.address);

fprintf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
fclose(fp);
}

void editRecord()
{
FILE *fp;
struct person obj;

fp=fopen("D:\\data.txt","r");
printf("\n============================");
printf("\n EDIT");
printf("\n============================\n\n");
while(!feof(fp))
{
fscanf(fp,"%s\n%s\n",obj.firstName,&obj.stdNumb);
printf("%s\n%s\n",obj.firstName,obj.stdNumb);
}
fclose(fp);
getch();
}

void searchRecord()
{
FILE *fp;
struct person obj;
char number[20];
int totrec=0;
fp=fopen("D:\\data.txt","r");
printf("\n\n============================");
printf("\n SEARCH");
printf("\n============================");
printf("\nEnter student number to search : ");
scanf("%s",&number);
while(!feof(fp))
{
fscanf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
if(strcmpi(obj.stdNumb,number)==0){
printf("\nStudent Number : %s",obj.stdNumb);
printf("\n\nName : %s",obj.firstName);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}

void deleteRecord()
{
FILE *fp, *fdel;
struct person obj;
char number[20];
fflush(stdin);
printf("\n============================");
printf("\n DELETE");
printf("\n============================\n\n");
printf("Enter student number to delete :");
scanf("%s", number);
fp=fopen("D:\\student.txt","r");
fdel=fopen("D:\\del.txt","w");
while(fscanf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address)!=0);
{
if(stricmp(number, obj.stdNumb)!=0)
fprintf(fdel,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
}
fclose(fp);
fclose(fdel);
remove("D:\\data.txt");
rename("D:\\del.txt","D:\\data.txt");

printf("Successully Deleted.");
getch();
}

void main()
{
char choice;
while(1)
{
printf("\n============================");
printf("\n MENU");
printf("\n============================");
printf("\n1. ADD");
printf("\n2. SEARCH");
printf("\n3. EDIT");
printf("\n4. DELETE");
printf("\n5. EXIT");
printf("\n============================");
printf("\n\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice)
{
case'1' : //call append record
appendData();
break;
case'2' : //call search record
searchRecord();
break;
case'3' : //edit record
editRecord();
break;
case'4' : //Read all record
deleteRecord();
break;
case'5':
exit(0);
defaut:
printf("ss");
}
}

}

这是我的完整代码。我已经完成了追加和搜索。问题是,当我尝试运行并删除记录时,输入学号后它不起作用。如何解决这个问题?

最佳答案

您尝试使用 scanf("%s", &number); 读取字符串,但 number 已经是一个指针,因此您需要编写 scanf (“%s”,数字);

当您通常尝试使用 scanf 读取某些内容时,您需要为其提供一个指向存储该值的位置的地址。

所以当我有类似的东西

int a;
scanf("%d", &a);

你给 scanf 一个a的地址。

char str[100];
scanf("%s", str);

这里 str 已经是一个指向 char 数组的指针。这就是为什么我们不需要添加地址运算符 &

但是当您添加地址运算符时,您会为 scanf 提供存储指针值的地址。

编辑:我在我的机器上用 clang 编译了您的代码,它警告我有关 7 scanf 语句:

a.c:20:11: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
scanf("%s",&obj.stdNumb);
~^ ~~~~~~~~~~~~
a.c:26:12: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
scanf(" %[^\n]s",&obj.lastName);
~^~~ ~~~~~~~~~~~~~
a.c:29:11: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
scanf("%s",&obj.icPass);
~^ ~~~~~~~~~~~
a.c:35:11: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
scanf("%s",&obj.gender);
~^ ~~~~~~~~~~~
a.c:41:11: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
scanf("%s", &obj.contact);
~^ ~~~~~~~~~~~~
a.c:44:12: warning: format specifies type 'char *' but the argument has type 'char (*)[30]' [-Wformat]
scanf(" %[^\n]s", &obj.address);
~^~~ ~~~~~~~~~~~~
a.c:61:19: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
fscanf(fp,"%s\n%s\n",obj.firstName,&obj.stdNumb);

还有三个错别字

a.c:83:6: warning: implicit declaration of function 'strcmpi' is invalid in C99 [-Wimplicit-function-declaration]
if(strcmpi(obj.stdNumb,number)==0){
^
a.c:112:8: warning: implicit declaration of function 'stricmp' is invalid in C99 [-Wimplicit-function-declaration]
if(stricmp(number, obj.stdNumb)!=0)
^
a.c:141:14: warning: implicit declaration of function 'getche' is invalid in C99 [-Wimplicit-function-declaration]
choice = getche();
^

关于c - 如何在C编程中删除记录中的特定行(无法进行删除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16456683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com