You are here:

C/Turbo C using fseek

Advertisement


Question
QUESTION: sir/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();
}


ANSWER: Hello Bryan.

I've had a look at your code and I believe the fundamental problem is that you're trying to seek to the employee code that was entered with scanf("%s",&cs.code); To make your program work, you have to enter not the employee code, but the "employee #" as generated by the 'i' variable in the file creation loop. The i variable is like a record number and you need to seek to the start of a record.

The fseek function does not search through the file for particular data. It positions the file pointer. The file pointer is an internal variable that specifies where the next read or write will be done in the file.

If you want to find a particular record based on the "Employee Code", you need to read through every record, and compare "worker.code" with the code you're looking for. There are ways to speed up this process if you keep the worker.txt file sorted by the employee code, but that is a more advanced idea.

There are a couple of other items to check in your code:
In the code is the worker struct. It should have
int rate;
not
int rate[30];

When opening the worker.txt file, the code specifies c:worker.txt. It is better to use c:\\worker.txt.

As a point of style, in C culture
#define s scanf
#define p printf
#define dy delay
would not be considered good style.

I hope that helps you. Feel free to ask follow-up question.
Best regards.
Zlatko




---------- FOLLOW-UP ----------

QUESTION: sir/madam thanks for the reply and also from some guidelines! Well i get your point based on your reply. So in the sequence of my code is no need to use fseek right??? all i need is to compare the employee code i entered and when its match this employee code from the data inside the worker.txt file then print it all details inside the employee code i input. sir/madam can u send or post that code without using fseek??? thanks in advance!!!

Answer
Hello Bryan.
You are correct that you should not be using fseek. The code to find a record based on the employee code is shown below. I have included full error checking as an example.

Best regards.
Zlatko

#include<stdio.h>
#include<string.h>

int no,i;
char choice;
typedef struct
{
   int rate;
   char name[30],rank[30],code[20];
}worker;

FILE *fil;
void clrstr(char st[])
{
   strcpy(st,"\0");
}
worker cs;

int main(void)
{
   no=i=0;
   clrscr();
   printf("Enter number of employees to process: ");scanf("%d",&no);
   fil = fopen("c:\\worker.txt","w+b");
   for(i=1;i<=no;i++)
   {
       printf("This is employee #%d\n",i);
       clrstr(cs.code);
       printf("Enter Employee code: ");scanf("%s",&cs.code);
       clrstr(cs.name);
       printf("Enter Employee name: ");scanf("%s",&cs.name);
       clrstr(cs.rank);
       printf("Enter Employee rank: ");scanf("%s",&cs.rank);
       printf("Enter Employee rate: ");scanf("%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++;
       delay(10000);
       printf("%s\t%s\t%s\t%d\n",cs.code,cs.name,cs.rank,cs.rate);
       fread(&cs,sizeof(cs),1,fil);
   }
   fclose(fil);

   /* Search for the desired employee code */
   {
       char desiredCode[20];
       int foundIt = 0;
       printf("Enter Employee code: ");
       /* I'm using fgets here, instead of scanf because scanf has no way to limit how many characters
       are read in. scanf is dangerous because it can cause memory corruption if user enters more
       characters than can be stored in the desiredCode. If you want to parse out individual words from
       the user input, you would use fgets followed by sscanf
       */
       if (fgets(desiredCode, sizeof(desiredCode), stdin) != NULL)
       {
         fil = fopen("c:\\worker.txt","rb+");
         if (fil != NULL)
         {
         do
         {
         if (fread(&cs,sizeof(cs),1,fil) == 1)
         {
         if (strcmp(cs.code, desiredCode) == 0)
         {
         printf("Found it\n");
         printf("%s\t%s\t%s\t%d\n",cs.code,cs.name,cs.rank,cs.rate);
         foundIt = 1;
         break; /* Break out of the while loop - you can use break, or you can rely on foundIt to quit the loop */
         }
         }
         else
         {
         int error = ferror(fil);
         if (error != 0)
         {
         clearerr(fil);
         printf("Error %d reading file\n", error);
         break; /* Break out of the while loop */
         }
         }
         } while( ! feof(fil) && ! foundIt);
         fclose(fil);
         if (! foundIt)
         {
         printf("Your choice of employee code '%s' was not found\n", desiredCode);
         }
         }
         else
         {
         printf("Cannot open the file\n");
         }
       }
       else
       {
         printf("OK, no employee code was entered. We're done!\n");
       }
   }

   getch();
   return 0;
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.