Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用文件IO和数组比较C语言中的字符串错误_C_Arrays_String_File Io_Strcmp - Fatal编程技术网

使用文件IO和数组比较C语言中的字符串错误

使用文件IO和数组比较C语言中的字符串错误,c,arrays,string,file-io,strcmp,C,Arrays,String,File Io,Strcmp,我的程序用于删除数据文件中的记录。我会要求用户输入他想要删除的“记录编号”。但是,当我使用strcmp这个函数来比较两个字符串时,它不起作用 问题:无法使用strcmp删除某些记录并将更新的数据放入新文件中 这是我的密码: #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ Del_menu(); return 0; } // function1 in

我的程序用于删除数据文件中的记录。我会要求用户输入他想要删除的“记录编号”。但是,当我使用strcmp这个函数来比较两个字符串时,它不起作用

问题:无法使用strcmp删除某些记录并将更新的数据放入新文件中

这是我的密码:

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

int main(){
    Del_menu();
    return 0;
}

// function1
int Del_menu() {
    while (1) {
          printf("Please enter a record number you wanted to delete : ");
          Del();
          printf("\nDo u want to delete  other record? Enter 'y' for yes and 
          'n' for no :");
          char ans;
          scanf(" %c", &ans);
          if (ans == 'N' || ans == 'n') {
          break;
          }
    }
}

//function2
int Del(){

        FILE *sfr = fopen("stock.txt", "r"); 
        //just for me getting total number,you can igone this
        char *str;
        int total;
        total=0;
    while (!feof(sfr)){
        str = fgetc(sfr);
        if(str == '\n') {
        total++;
        }
    }
        total = (total/9);
        fclose(sfr);

        FILE *fileptr1, *fileptr2;
        char filename[40] = "data.txt";
        int total = 0;  
        int  total_1 = 0 ,total_2 = 0, i = 0 , temp;

   char itemrecord [40][40];
   char quantity [40][40];
   char weight [40][40];
   char itemname [40][40];
   char catagory [40][40];
   char recipient [40][40];
   char final_destination [40][40];
   char status [40][40];

        fseek(stdin, 0, SEEK_END);
        fflush(stdin);

        i++;
        total--;
        }

        FILE *fileptr1, *fileptr2;
        char filename[40]="stock.txt";

        fileptr1 = fopen(filename, "r");

        char buffer [50];
    while(total_1!=0){

  fgets(recordnum[i],50,fileptr1);
  fgets(itemname[i],50,fileptr1);
  fgets(itemrecord[i],50,fileptr1);
  fgets(catagory[i],50,fileptr1);
  fgets(quantity[i],50,fileptr1);
  fgets(weight[i],50,fileptr1);
  fgets(recipient[i],50,fileptr1);
  fgets(final_destination[i],50,fileptr1);
  fgets(status[i],50,fileptr1);
  fgets(buffer, 50,fileptr1);
        fclose(fileptr1);
        fclose(fileptr2);
        i++;
        total_1--;
  }

        char del_data[41]; //get user input
        fgets(del_data,50,stdin);
        del_data[strlen(del_data) - 1] = '\0';
        printf("\nyou have entered :%s\n", del_data);

        printf("\nRecord in file before delete:\n");
        printf("Total record: %d\n",total);

        i=0;
    while(total_2!=0){

       printf("%s%s%s%s%s%s%s%s%s\n",recordnum[i],itemrecord[i],quantity[i],
       weight[i],itemname[i], catagory[i],recipient[i],
       final_destination[i],status[i]);
       i++;
       total_2--;
    }

       rewind(fileptr1);

       fseek(stdin, 0, SEEK_END);
       fileptr2 = fopen("copy.c", "w");  //stored the data after deleted
       total_3=total_3 -1;

       i=0; 
    while(total!=0){

       if ( strcmp(recordnum[i],del_data) != 0){
       printf("%s",recordnum[i]); //for me checking is it successful              

       fprintf(fileptr2,"%s%s%s%s%s%s%s%s%s\n",recordnum[i],itemrecord[i],
       quantity[i],weight[i],itemname[i], catagory[i],recipient[i],
       final_destination[i],status[i]);

       i++; total--;}
            }


remove(filename);
// rename the file copy.c to original name
rename("copy.c", filename);
} // end of function 2

读取用户的选择时,要小心删除
fgets()
存储在缓冲区中的尾随换行符,但不要对读取到数组中的数据应用相同的处理方法。文件中的每个字段似乎都位于单独的行上,因此在读取到数组中的数据中确实存在新行。因此,比较失败,因为字符串确实不同


最简单的解决方案是停止从用户输入中剥离尾随的换行符。这段代码还有很多其他问题,但进行更改至少应该能让它在您所问的方面正常工作。

两个问题(与您的问题无关):;在C规范中,对只输入的流调用
fflush
(如
stdin
)被明确称为未定义行为。然后,一个可能相关的问题是:您最多可以将50个字符(包括终止符)读入40个字符的数组。您的输入在哪里?哦,而
str
是指向
char
的指针。该函数以
int
的形式返回单个字符。您还可以将此指针与常量字符文字进行比较。编译器应该为此向您发出警告,否则您需要启用更多警告并将其视为错误。@HangWui您有很多垃圾代码,并且没有正确缩进。
Please enter a record runber you want to delete: 1001

You enterd: 1001

Record in file before deleted:
Total:3
1001
Orange Laptop Computer DX5
235524
Electronics
1
1.8 kg
Chan 
Mong Kok
Delivery

1002
Japanese Garden Pear Gift Box
300522
Food
2
4.2 kg
Cheung 
Yuen Long
Arrival

1003
Koppo Badminton Racket GPX-15
77524
Fashion
3
0.6 kg
Lee Siu Yu
Fortress Hill
Warehouse

1001 // not exist after i finish this code
1002 // just for I debugging now
1003 // here is my problem* mean that cannot delete record successful

DO you want to delete other record?Enter'y' for yes, 'n' for no: n