Link to home
Start Free TrialLog in
Avatar of SIMK
SIMK

asked on

accessing particular position on file

How to locate the position on file, ??
suppose i have a file called sub.txt which has the subject names of different semesters,
now from that file i want to read the subject name of particular semester only, how this can be done, and semester wise the number of subjects can also differ , ok gyes please help me out of this problem ???
Avatar of Jase-Coder
Jase-Coder

first you could read the whole file into some array of other structure, secondly you could use the fseek() function to access a speific section of the file
you may go throught the filie like
int main()
{
 fp=fopen("sub.txt","rt");
 while(!feof(fp))
 {
  fscanf(fp,"%s",s);
  if(NULL==strstr(s,"specialsemester")) continue;
  break;
 }
 if(!feof)
 {
  printf("ok I found it\n");
  //doing something
 }
}
Avatar of Kent Olsen
Hi SIMK,

You need to build a structure (table) that contains the key values in the file and their locations.  Only then can you use the data.

Here's a function to build the table.  It's up to you to use it.  (Homework, and all that.... :)  )

Kent




#define MAX_TABLE 100                 /*  Maximum number of semesters to track  */
#define MAX_LINE_LENGTH 256

long FileOffset[MAX_TABLE];
char *Title[MAX_TABLE];
int   Semesters = 0;

FILE *input = NULL;

LoadSemesterHeaders (void)
{
  char Text[MAX_LINE_LENGTH];
  long  FilePosition;

  input = fopen ("Subjects.txt", "rt");  /*  Do sanity checks on *input*  */
  while (1)
  {
    FilePosition = ftell (input);
    fgets (Buffer, MAX_LINE_LENGTH, input);
    if (feof (input))
      break;

    if (This_Line_Is_A_Subject_Header ())
    {
      if (Semesters+1 >= MAX_TABLE)   /*  Too many headers for table  */
        break;                                        /*  Print a nice message first  */
      FileOffset[Semesters] = FilePosition;
      Title[Semesters] = strdup (Text);
      Semesters++;
    }
  }
}
hi
you can have a look at "fseek" , "rewind" etc. functions also
ASKER CERTIFIED SOLUTION
Avatar of aravindtj
aravindtj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial