There are so many things to look after.
1. Files are not closed properly.
You have opened the file at the beginning of the program. and have never closed. Anything you write into a file does not immediately written actually, they are buffered. You need to flush it or close the file. So, before you do any other operation on the file, be sure it is flushed.
2. Records are not read back from file.
You have not read back the records from file. You always searching the current values of rec structure. You need to read each record to the structure before comparing. Also, if you do not read the file, the file pointer never advances, and the control stays in the loop forever.
3. New lines are not handled.
You have additional new lines after each records. You need to handles those while reading back records from the file. gets consumes one newline at the end of the line. So you need to manually handle the additional \n.
Additionally, you need to break from the while loop if you find your record. There is no point staying there unless you expect duplicates.
The following has some modification over your code. This works for me. You may try this:
#include <stdio.h> #include <stdlib.h> #include <string.h> main(){ struct info{ char name[40]; char sur[40]; }; struct info rec; FILE *f1, *f2; int sel, ser, res; char cmp[40]; char dummy; int cont=0; do{ do{ printf("1> Add account\n"); printf("2> Search account\n"); printf("3> Modify account\n"); printf("4> Exit\n"); printf("Type your choice -> "); scanf("%d", &sel); if(sel<1 || sel>4){ printf("ERROR: The choice isn't allowed\n"); } }while(sel<1 || sel>4); getchar(); switch(sel){ case 1: printf("Insert new account\n"); printf("Write name: "); fgets(rec.name, sizeof(rec.name), stdin); printf("Write surname: "); fgets(rec.sur, sizeof(rec.sur), stdin); f1=fopen("lis.txt","a+"); //<- open file for writing fputs(rec.name,f1); fputs(rec.sur,f1); fprintf(f1,"\n"); fclose(f1); // close the file. this will flush the buffer. printf("Account added!\n"); break; case 2: printf("Search account\n"); printf("Write surname to search: "); fgets(cmp, sizeof(cmp), stdin); f1=fopen("lis.txt","r"); //<- open the file for reading while(!feof(f1)){ fgets(rec.name,sizeof(rec.name),f1); //<- read both the data. this will update the file pointer. fgets(rec.sur,sizeof(rec.sur),f1); fscanf(f1,"%c",&dummy); //<- this handles the additional newline if(strcmp(cmp,rec.sur)==0){ printf("RECORD FOUND::\nName:%sSurname:%s\n\n",rec.name,rec.sur); break; //<- break if record is found } } fclose(f1); //<- close the file after you are done. break; // case 3: // printf("Modify account\n"); // //funzione ricerca qua // printf("Account modificato correttamente!\n"); // break; case 4: printf("Closing...\n"); break; default: printf("ERROR!\n"); break; } }while(sel!=4); }
NOTE: As we are using feof() to exit the loop, we have to read the EOF to fulfil the exit condition. Hence in this code the loop continues one more time after the last record where EOF is read from the file. This is not a problem unless you are trying to print all the records. In that case use a different exit condition.
while (fgets(cmp, sizeof(cmp), stdin) != 0) { ... }. You only usefeof()after an I/O function has reported EOF (e.g.fgets()returned NULL) to distinguish between EOF and an I/O error. Basically, if you forget thefeof()exists, you won't go far wrong.feof()problem.