0

I'm doing a school project, and my focus is to copy several lines of a text file to an arrays of strings and then ask to the user which string they want to remove and write back the array on a file.

My program is opening the file and copying the content right to the array, but when I try to compare line by line with the string that the user gave me it doesn't go so well.

If I have in the file pedropedro and I want to remove it I need to compare all the elements of the array with pedropedro, I'm using strcmp, but it never returns 0 so I can't delete this line. Why is this happening? Stay safe.

if (strcmp("6",numero)==0)
    {
            char nome_elim[15];
            char pass_elim[15];
            char final_elim[30];
            char c;
            int i=0;
            int tamanho;
           printf("Insira o nome de utilizador que pretende eliminar:\n");
           scanf("%s",nome_elim);
           printf("Insira a password referent ao nome de utilizador:\n");
           scanf("%s",pass_elim);
           strcat(final_elim,nome_elim);
           strcat(final_elim,pass_elim);
           strcat(final_elim,"\n");
           
           printf("Vai adicionar: %s\n",final_elim);
           
           FILE *fptr =fopen("logins.txt","r+");
                                    
          if(fptr == NULL)
           {
              printf("Error!");   
             exit(1);             
           }else{
           char ch;
           int count =0;
           do{
           ch=fgetc(fptr);
           if(ch=='\n')count++;
           
           }while(ch!=EOF);
           rewind(fptr);
            tamanho=count;
           int i;
           for(i=0;i<count;i++){
           fscanf(fptr,"%s",cur_member[i].a);
           printf("%s\n",cur_member[i].a);
           
           }
           
           }
           int pos=0;
                   
           
           
              fclose(fptr);
           
           
           
                        
            
            FILE *fptr1 =fopen("logins.txt","w");
                                    
          if(fptr == NULL)
           {
              printf("Error!");   
             exit(1);   
             }
                
            
           for(int y=0;y<tamanho;y++)
          {
           if(strcmp(cur_member[y].a,final_elim)==0)
           {
           printf("IGUALIGUAL");
           continue;
           }
           else{
           printf("%d",y);
           fputs(cur_member[y].a,fptr1);
           fputs("\n",fptr1);
           }
           
           }
            fclose(fptr1);     
        
        strcpy(nome_elim,"\0");
        strcpy(pass_elim,"\0");
        strcpy(final_elim,"\0"); 
         
           
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • `strcat(final_elim,nome_elim);` the contents of `final_elim` are uninitialized at this point - this results in [undefined behaviour](https://en.cppreference.com/w/c/language/behavior). Zero-initialize the array beforehand, e.g., `char final_elim[30] = { 0 }`, or change this first `strcat` to a `strcpy`. Note however, `scanf("%s", ...);` is [as dangerous as `gets`](https://stackoverflow.com/q/1694036/2505965). – Oka May 07 '22 at 12:16
  • What is the definition of `cur_member`? Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Oka May 07 '22 at 12:19
  • `strcat(final_elim,"\n");` - the search string would have a newline appended to it. `fscanf(fptr,"%s",cur_member[i].a);` will never read a newline. How can these match? – Oka May 07 '22 at 12:23

0 Answers0