in below codes i am getting only one record...rather than i need to find all one
The file you give contains external representations, not dumps of the (hidden) struct, you cannot use fread to read the contents.
You say you got one, the format indicates one field is a _float, it is impossible you read right a float value, also the read name cannot contains miraculously a null character to end it so strcmp will not returns 0, and it is also very probably impossible the size of your struct is compatible with the size of each line newline included because they seems have different length. For me you never find the expected record.
The else branch printing "Succesfully printed everything" has no sense, this is not because strcmp does not return 0 that you finished to read all the records then printed everything. In that case you also call getch whose blocks the execution in case all buffered inputs was read.
The rewind is useless because you just open the file, so you are at its beginning.
You read the name of the record using gets, that function is deprecated sinse a long time because very dangerous, it has no protection against the fact you write the input out of the receiving array of character.
A way can be (the file is given in parameter rather than to have its pathname given through a literal string in the code) including a probable definition of the struct compatible with the code you give :
#include <stdio.h>
#include <string.h>
typedef struct Record {
char name[50];
char date[10];
float amount;
} Record;
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage: %s <file>\n", *argv);
else {
FILE * fp = fopen(argv[1],"r");
if (fp == NULL)
fprintf(stderr, "cannot open \"%s\"\n", argv[1]);
else {
char emp[50];
printf("Name of record : ");
if (scanf("%49s", emp) == 1) { /* or use fgets then remove the newline */
Record a;
while (fscanf(fp, "%49s %9s %f", a.name, a.date, &a.amount) == 3) {
if(strcmp(a.name, emp)==0)
printf("%s %s %f\n", a.name, a.date, a.amount);
}
puts("all was read");
/* add getch(); if you want */
}
fclose(fp);
}
}
}
with your file babarecord.dat, compilation and executions
gcc -pedantic -Wall -Wextra -Werror c.c
pi@raspberrypi:/tmp $ ./a.out
Usage: ./a.out <file>
pi@raspberrypi:/tmp $ ./a.out babarecord.dat
Name of record : aze
all was read
pi@raspberrypi:/tmp $ ./a.out babarecord.dat
Name of record : sam
sam 565 656.000000
sam 789 5658.000000
all was read
pi@raspberrypi:/tmp $ ./a.out babarecord.dat
Name of record : ram
ram 7565 5686.000000
all was read
pi@raspberrypi:/tmp $
P.S. 565, 7565 and 789 are strange dates