Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Random Access Files

Status
Not open for further replies.

Ahrens

Programmer
Aug 17, 2000
2
CA
I'm writing a program that needs to randomly access a text file. All the books I've read have not been helpful.

Right now the user enters a label code and I search the file sequentially for a match. Because I'm using text files (binary files are not possible - the files are created in a BBx environment) I'm using fscanf and fprintf to read and write to the file. I'd like to use a random search for the label code to speed things up. As far as I have read I can't use fread & fwrite with text files. My biggest problem is that I can't find an example of a random search that doesn't use the record number. I need to search by the label code not the position of the record in the file.

Any tips/help or book references would be appreciated. Thanks in advance.

Heather
 
This program uses the strstr function from the string.h
library.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 100

void main()
{
FILE *fnew, *fold;
char buf[BUFSIZE];
char Inbuf[BUFSIZE];
char Outbuf[BUFSIZE];
char file_new [27] = &quot;E:\\autostart\\pace98_A.txt&quot;;
char filename_old [27] = &quot;E:\\autostart\\pace95_A.txt&quot;;

//printf (&quot;%s \n&quot;,file_new);

if ((fnew = fopen(file_new,&quot;w&quot;))==NULL)
{
fprintf(stderr, &quot;Error opening file %s \n&quot;, file_new);
exit(1);
}

if ((fold = fopen(filename_old,&quot;r&quot;))==NULL)
{
fprintf(stderr, &quot;Error opening file %s \n&quot;, filename_old);
exit(1);
}
else
{
while (!feof(fold))
{
fgets(buf, BUFSIZE, fold);
//printf(&quot;%s&quot;, buf);
// code to check for functions only
if ( (!( strpbrk(buf, &quot;'&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;;&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;,&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;.&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;$&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;?&quot;)) != NULL) &&
(!( strpbrk(buf, &quot;*&quot;)) != NULL) &
( ( strpbrk(buf, &quot;:&quot;)) != NULL) )
{
//printf(&quot;%s\n&quot;, buf);
//fprintf(fnew, &quot;%s \n&quot;, buf);
}

// code to pick info you want
/* ************
if( (( strstr(buf, &quot;matrix&quot;)) != NULL)||
(( strstr(buf, &quot;kl&quot;)) != NULL)||
(( strstr(buf, &quot;Matrix&quot;)) != NULL)||
(( strstr(buf, &quot;Add_mat&quot;)) != NULL)||
(( strstr(buf, &quot;Mat_&quot;)) != NULL)||
(( strstr(buf, &quot;K&L&quot;)) != NULL) )
* ************/

if (( strstr(buf, &quot;SUB&quot;)) != NULL)
{
printf(&quot;%s\n&quot;, buf);
fprintf(fnew, &quot;%s \n&quot;, buf);
}
else
{
//printf(&quot;null\n&quot;);
}

}//end while
}// end else

fclose(fold);
fclose(fnew);

}// end of main
 
> I'd like to use a random search

Well, as far as I am aware there is not such thing. My belief would be that there is no such thing as a 'random search' because it doesn't make any sense.

Could you explain what a random search is?

-pete
 
By random access I mean begin processing a file at a specified location within the file. When randomly processing a file, a program must know its exact position in the file. Using fseek() I can ask the user to input the record number they wish to view and then go directly to that particular record. Otherwise, the program must go through each record in the file sequentially, one record at a time, which take a long time with 5,000 records. But I can't use the record number because the user won't know the record number. They will request a search by label number which are 9 digit numbers - eg 001200367.

I have a file with 5,000 records, each with the first record being a unique label number. I would like to get the label number inputted by the user without going through the file sequentially. Right now, I sequentially search the file(fscanf), comparing (strcmp) the label # read to the label # that the user is requesting to view. In some cases, depending on how big the file is (it varies), this process takes too long.

Thanks
 
Simply create a record at the end of file containing an index of &quot;label numbers&quot; to &quot;record numbers&quot;. Then all you have to do is seek to the last record to get the record index for the &quot;label number&quot;....
 
Good book:
Advanced C procramming by example /John W. Perry
ISBN 0-534-95140-6
There are a few interesting tricks, about random access too.

WARNING:
When I was trying to use return value of ftell on NT, but working with UNIX ASCII file, for fseek it didn't work because of difference in the end string character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top