I'm exercising with C and file management, I'm able to open a file, write a record on the file, close the file, but I have problem to find an already written record. This is my exercise: (The search in case 2)
#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];
int cont=0;
f1=fopen("lis.txt","a+");
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);
fputs(rec.name,f1);
fputs(rec.sur,f1);
fprintf(f1,"\n");
printf("Account added!\n");
break;
case 2:
printf("Search account\n");
printf("Write surname to search: ");
fgets(cmp, sizeof(cmp), stdin);
while(!feof(f1)){
if(strcmp(cmp,rec.sur)==0){
printf("ENT\n");
}
}
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);
}
The programme isn't finished, so there are plenty of unused things that I'll fix later. It is tested on OpenVMS.