Solved

accessing particular position on file

Posted on 2004-09-20
5
186 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
5 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
Comment Utility
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
Comment Utility
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:Kdo
Comment Utility
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
Comment Utility
hi
you can have a look at "fseek" , "rewind" etc. functions also
0
 
LVL 3

Accepted Solution

by:
aravindtj earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stack 22 155
How do I avoid pointer to integer casting errors in C programming? 4 189
Inorder binary search tree 5 153
C hashtable library 3 71
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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now