I am a bit lost trying to read stuff from a file which has been written before.
I want my program to open an already existing file which has my friends ‘names and numbers written like this:
friend1-7372384729
friend2-9374628202
…
The input to the program will be my friend´s name and the output after searching in the file will be his number.
I have studied these functions: fscanf, fgetc, and fgets and tried to use them but it isn’t working , I must be doing something wrong.
Here is the code: I hope it isnt as awful as I think it is
Code:#include <stdio.h> #include <string.h> #define d 25 int main(){ char nom[d],*a; int i=0,n; FILE *f; printf("\nIntroduce your friend´s name: "); gets(nom); if((f=fopen("telefonos.txt","r"))==NULL) printf("\nError oppening the file"); while((i!=EOF)&&(a!=nom)) i=fscanf(f,"%s",a); if(i==EOF) {printf("\nIt seems you haven´t got %s number registered",nom);return 0;} printf("\n%s number is: %d", *a, n); fclose(f); return 0;}



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.