Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

accessing particular position on file

Posted on 2004-09-20
5
Medium Priority
?
202 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 46

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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

721 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