I have this kind of struct:
typedef struct {
char a[MAX];
char b[MAX];
int c;
} mys;
and this functions:
mys* search_m(FILE *fp, int m)
{
mys* s;
s=(mys *)malloc(sizeof(mys));
if(s){
for(int i=0;i<m;i++)
fread(s,sizeof(mys),1,fp);
}else{
printf("Error during allocation\n");
}
return s;
}
void search_s(FILE *fp, char *c)
{
mys s;
int count=0;
while(fread(&s,sizeof(mys),1,fp)==1){
if(strcmp(s.b,c)==0){
show_s(s,count);
}
count++;
}
return;
}
that's a part of my program. During the input fase (when I write data on my file) I don't have any problem ( using fread) but I have some problem reading it.
The my function search_s execute and find the value every time that I call it, but the search_m function seems that can't read anby data in my file and since it's practically the same things that I did on search_s I don't know where is my mistake.
In my main when I call seach_m I do this:
s=*search_ID(f_ptr,m);
show_s(s,m);
I don't think that the problem is in the main since the fread in the search_m function doesn't load any data (it stops on the first try).
Where is my mistake? I thought that it was all right PS before calling this function I open the file and I check for errors and after I close the file.
EDIT: I'm opening the file with this:
f_ptr=fopen(argv[1],"rb");
(It doesn't work (also) if I only use "r") argv[1] is the name of my file
PS m is the number of the element in the file that I want to read. (I read every time one block untile I don't get the m-esimo element)
My file contains only struct of that type.