-3

My programme is used for delete record in data file. I will ask the user input the "record number" that he want to delete.However, when i using strcmp this function to compare two string, it doesn't work.

Problem:cannot using strcmp delete certain record and put updated data in the new file

Here is my code:

#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

Result:

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
Hang Wui
  • 27
  • 5
  • 3
    Two problems (unrelated to your question): [Don't call `feof` in a loop condition](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong); And calling `fflush` on an input-only stream (like `stdin`) is explicitly mentioned in the C specification as *undefined behavior*. – Some programmer dude Nov 29 '18 at 14:04
  • 2
    Then one problem that could be related: You read up to 50 characters (including terminator) into arrays of 40 characters. – Some programmer dude Nov 29 '18 at 14:04
  • 1
    Where is your input? – Swordfish Nov 29 '18 at 14:05
  • Also, `stdin` is probably not seekable, so your `fseek()` is probably failing (you do not check that). I'm uncertain what you intended it to accomplish. – John Bollinger Nov 29 '18 at 14:07
  • 1
    Oh, and `str` is a pointer to `char`. The [`fgetc`](https://en.cppreference.com/w/c/io/fgetc) function returns a single character in the form of an `int`. You also compare this pointer with a constant character literal. The compiler should be shouting warnings at you for that, and if not you need to enable more warnings and treat them as errors. – Some programmer dude Nov 29 '18 at 14:07
  • @Swordfish u can see my code `char del_data[41]; //get user input` – Hang Wui Nov 29 '18 at 14:08
  • 1
    @HangWui You have lot of junk code and not properly indented. – kiran Biradar Nov 29 '18 at 14:09
  • 1
    Lastly, your code as you show it now, shouldn't even *compile*! Large parts of your code is *outside* of any function. Have you really attempted to build what you show? Perhaps you should try to create an [mcve] to show us? – Some programmer dude Nov 29 '18 at 14:10
  • my fseek() used for clear buffer, however i forget delete `fflush(stdin)`. – Hang Wui Nov 29 '18 at 14:18
  • 1
    `fseek()` is no more likely to have an effect on `stdin` than is `fflush()`. It's probably *less* likely, in fact. If you want to clear pending input, then the only standard way to do so is to *read* it. – John Bollinger Nov 29 '18 at 14:22
  • ok, thank you all useful comments. I try to improve my program – Hang Wui Nov 29 '18 at 14:26
  • You haven't even fixed all the problems we pointed out from the **last** time. You were specifically told not to `fflush(stdin)` https://stackoverflow.com/questions/53448384/c-programming-how-can-i-put-text-file-content-into-my-array#comment93769490_53448384 – torstenvl Nov 29 '18 at 14:29
  • @torstenvl i am so sorry for that , so i use fseek()instead of fflush() and i forget remove fflush – Hang Wui Nov 29 '18 at 14:50
  • Anyway, thank all your meaningful reply to improve my stupid code – – Hang Wui Nov 29 '18 at 14:58
  • *Swordfish u can see my code `char del_data[41]; //get user input`* – And now I'm supposed to guess what $user entered? thanks. – Swordfish Nov 29 '18 at 16:06

1 Answers1

1

When you read the user's selection, you are careful to remove the trailing newline that fgets() stores in the buffer, but you do not apply the same treatment to the data that you read into your array. Your file appears to have each field on a separate line, so there are indeed newlines in the data that you read into your array. The comparison therefore fails because the strings are indeed different.

The simplest solution would be to stop stripping the trailing newline from the user input. There are a lot of other problems with this code, but making that change should at least get it working in the regard you asked about.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157