-2
#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");
    }
}

}

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

4

1 回答 1

1

您尝试使用scanf("%s", &number);number已经是指针读取字符串,因此您需要写入scanf("%s", number);.

当您通常尝试读取某些内容时,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();
             ^
于 2013-05-09T07:42:42.753 回答