SIMK
asked on
accessing particular position on file
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 ???
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 ???
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
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,"special semester") ) continue;
break;
}
if(!feof)
{
printf("ok I found it\n");
//doing something
}
}
int main()
{
fp=fopen("sub.txt","rt");
while(!feof(fp))
{
fscanf(fp,"%s",s);
if(NULL==strstr(s,"special
break;
}
if(!feof)
{
printf("ok I found it\n");
//doing something
}
}
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_He ader ())
{
if (Semesters+1 >= MAX_TABLE) /* Too many headers for table */
break; /* Print a nice message first */
FileOffset[Semesters] = FilePosition;
Title[Semesters] = strdup (Text);
Semesters++;
}
}
}
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_He
{
if (Semesters+1 >= MAX_TABLE) /* Too many headers for table */
break; /* Print a nice message first */
FileOffset[Semesters] = FilePosition;
Title[Semesters] = strdup (Text);
Semesters++;
}
}
}
hi
you can have a look at "fseek" , "rewind" etc. functions also
you can have a look at "fseek" , "rewind" etc. functions also
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.