Solved

accessing particular position on file

Posted on 2004-09-20
5
201 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

Industry Leaders: 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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

724 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