-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);

そして3つのタイプミス

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 に答える