C/Turbo C using fseek
Expert: Joseph Moore - 8/26/2009
Questionsir/madam i need your help regarding this issue...How to use fseek function to search some file in a group of files in a txt file?
From this time we will be using char string but not integer to seek pls take a look of this code..
#include<stdio.h>
#include<string.h>
#define s scanf
#define p printf
#define dy delay
int no,i;
char choice;
typedef struct
{
int rate[30];
char name[30],rank[30],code[20];
}worker;
FILE *fil;
void clrstr(char st[])
{
strcpy(st,"\0");
}
worker cs;
main()
{
no=i=0;
clrscr();
p("Enter number of employees to process: ");s("%d",&no);
fil = fopen("c:worker.txt","w+b");
for(i=1;i<=no;i++)
{
p("This is employee #%d\n",i);
clrstr(cs.code);
p("Enter Employee code: ");s("%s",&cs.code);
clrstr(cs.name);
p("Enter Employee name: ");s("%s",&cs.name);
clrstr(cs.rank);
p("Enter Employee rank: ");s("%s",&cs.rank);
p("Enter Employee rate: ");s("%d",&cs.rate);
fwrite(&cs,sizeof(cs),1,fil);
}
fclose(fil);
clrscr();
fil = fopen("c:worker.txt","r+b");
fread(&cs,sizeof(cs),1,fil);
i=0;
while(!feof(fil))
{
i++;dy(10000);
p("%s\t%s\t%s\t%d\n",cs.code,cs.name,cs.rank,cs.rate);
fread(&cs,sizeof(cs),1,fil);
}
fclose(fil);
i=0;
fil = fopen("c:worker.txt","rb+");
p("Enter Employee code: ");
s("%d",&i); <<<<<~~~~at this point we need to input s("%s",&cs.code); or anything just not to use int i;because we need to input employee code not from what number this employee code exist in a txt file!
fseek(fil,(i-1)*sizeof(cs),0); <<<~~~~at this point using fseek we do not use (i-1)*sizeof(cs) because we are going to input employee code not a number that employee code exist in a txt file!
fread(&cs,sizeof(cs),1,fil);
p("%s\t%s\t%s\t%d\n",cs.code,cs.name,cs.rank,cs.rate);
fclose(fil);
getch();
}
AnswerHi, Bryan.
This isn't really a case where you would use fseek (sort of, I'll explain in a bit).
You're searching for a particular employee code, which is a string. This means you need to search the employee records for the employee with that string as their code. Since you do not have a method of loading all employees at once, you will need to search them linearly in the file.
The easiest method to write would be to simply re-use your previous loop that loads every employee. Then, instead of displaying the record, check the code against the one entered by the user. If you have found the requested employee, then you can display their record and end the loop. If the loop terminates without ever having found the record, then you can display an error. The code to do this would look something like:
printf("Enter Employee code: ");
scanf("%s",&code); // code was previously declared as a char[20]
fil = fopen("c:\\worker.txt","r+b");
fread(&cs,sizeof(cs),1,fil);
i=0;
while(!feof(fil))
{
if (!strcmp(cs.code, code))
{
i = 1; // setting a flag to mark that the item was found
printf("%s\t%s\t%s\t%d\n",cs.code,cs.name,cs.rank,cs.rate);
break;
}
fread(&cs,sizeof(cs),1,fil);
}
fclose(fil);
if (i == 0)
{
// employee wasn't found
printf("Employee %s does not exist.\n", code);
}
If you *really* want to use fseek, then you can use it to skip around the file and read only the employee codes until you find what you're looking for. To do that, you'd need to fseek by the number of bytes that precede the code section of the structure, then read the code and check it against the requested code. If it matches, then fseek back to the start of the current entry, read the entry in its entirety, and print it out. Otherwise, move on to the next code. It's trickier to implement, but it means less reading from disk.
On a side note, you need to remove the [30] from the rate member of the structure, and there is no reason to declare all of your variables as globals. Simply declare them at the beginning of the main function, instead.
Ok, I hope my answer has helped you, and if you have further questions, do not hesitate to ask.