Solved

accessing particular position on file

Posted on 2004-09-20
5
198 Views
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 ???
0
Comment
Question by:SIMK
[X]
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
5 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
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
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12103972
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
 }
}
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 12105398
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++;
    }
  }
}
0
 
LVL 6

Expert Comment

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

Accepted Solution

by:
aravindtj earned 500 total points
ID: 12125046
Hi,
 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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

761 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