Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

accessing particular position on file

Posted on 2004-09-20
Last Modified: 2010-04-15
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 ???
Question by:SIMK
LVL 11

Expert Comment

ID: 12103923
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

Expert Comment

ID: 12103972
you may go throught the filie like
int main()
  if(NULL==strstr(s,"specialsemester")) continue;
  printf("ok I found it\n");
  //doing something
LVL 45

Expert Comment

by:Kent Olsen
ID: 12105398

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.... :)  )


#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))

    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);

Expert Comment

by:Amritpal Singh
ID: 12109505
you can have a look at "fseek" , "rewind" etc. functions also

Accepted Solution

aravindtj earned 500 total points
ID: 12125046
 try to use structures to write the data.
 you can access the data writen in the file easily as structure.
 use simple open, read, write methods. To locate a particular position, use lseek method.
 It is fastest method and compatible to Win & Unix plotform.
 To locate a particular record, you have to get the structures from the file till you get the correct value.
 you can also store the location of the particular value in a variable and can access to frequently needed value easily.

Bes of luck.

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux context switch  - loop takes long time to process 6 128
Concatenate two strings Last and First Name 10 60
Using ANSI C how to Read a .csv file 10 93
How to learn Linux? 10 66
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question