>>Return value you mean if its zero its an error ????
Look:
Code:
while (fread(&Data, sizeof( struct data), 1, dPtr) == 1)
{
/* Process data */
}
>>Why do i have to open it in binary mode
Because you're writing structures. And yes, it is standard C.
>>fflush(stdin);
Come on, you know better than that!
... and the problem with the search facility is.... you are using fgets() to get the input data in the first place, and scanf() to get the search criteria. If you don't know what's wrong with doing this and what problems it causes, I suggest you do a board search
Also, you have some serious problems with going outside array bounds:
Code:
while ( ++cnt <= datasize )
{
fprintf (fpdebug, "Start of for loop\n");
for (i = 0; i <=num; i++ )
{
fprintf (fpdebug, "Comp: >%s< >%s<\n", Data[i].firstname, input );
if ( strcmp(Data[i].firstname, input ) == 0 )
{
printf("%s%s%s%s", Data[i].firstname, Data[i].lastname, Data[i].address, Data[i].phone);
}
}
i+=i;
num = i+ num;
gotoxy(1,25);
cprintf("Press <return> to continue....");
getch();
}
The above code has two debug lines I added to write out some variables to see what was going on. My phone book had only one record in it, so I was mildly suprised when I found that the loops had checked a few more records than the one:
Code:
Start of for loop
Comp: >hammer< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Start of for loop
Comp: >hammer< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<
Comp: >< >hammer<