【问题标题】:How to delete a specific line from record in C programming (it can't proceed deletion) [closed]如何在 C 编程中从记录中删除特定行(无法继续删除)[关闭]
【发布时间】:2013-05-09 07:31:14
【问题描述】:
#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");
    }
}

}

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

【问题讨论】:

  • 您可能需要向我们提供更多信息。为什么“它不能继续?”
  • 如果我输入了学号作为输入,它不会删除记录..

标签: c search replace report


【解决方案1】:

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

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

所以当我有类似的东西时

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

你给scanfa的地址。

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

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

但是,当您添加地址运算符时,您给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();
             ^

【讨论】:

  • 我改了,还是不行。。也许不是问题。。
  • scanf("%s",&amp;obj.stdNumb);scanf(" %[^\n]s",&amp;obj.lastName); 你又来了
  • 但是,在这里我可以运行我的程序..问题只在删除菜单..
  • @Monica Panjaitan 编译器警告意味着可能有问题,但编译器无法查看是否正常。所以它会编译代码,因为编译器说用户知道他在做什么。
  • 现在,我可以删除记录,但是在指定行时仍然有问题..如果我在文件中有 5 个学生,并且我想删除学生 2,它将删除除学生 1 之外的所有记录,但如果我想删除学生 1,它将删除所有记录.. 如何解决?谢谢:)
猜你喜欢
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 2021-05-08
  • 2011-02-14
相关资源
最近更新 更多