Solved

Seeking Positions in a File

Posted on 1999-01-29
10
288 Views
Last Modified: 2010-04-15
Hi !

I am trying to seek positions in a file.  The file is about 5 meg in size, and in one process need to seek about 200 times.  I am currently using fopen and fseek to do this and it is taking about 6-8 seconds to complete.  I need to speed this up to be as fast as possible.  Also when this is being done simultaneously by about 10 people it can take longer than 20 seconds per process.  I would be grateful if you could advise me on any faster ways of doing this.

Regards,

Marvin.
0
Comment
Question by:checkin
  • 6
  • 3
10 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1258516
Can you post your code since a fseek should not need so much time!
0
 

Author Comment

by:checkin
ID: 1258517
Below is the chunk that records the time elapsed.

char tmpStr[4096];

a = time(&a);

idxFile = fopen("MainData.txt","r");
for(i=1;i<=posCounter;i++) {
  tmpPosition = atoi(read_record(inBuff,i,','));
  fseek(idxFile,tmpPosition, 0);
  fgets(tmpStr,sizeof(tmpStr),idxFile);
}
fclose(idxFile);

b = time(&b);
diff = b - a;
printf("Get Records Time = [%d] Seconds<br>\n",diff);



0
 
LVL 10

Expert Comment

by:rbr
ID: 1258518
I guess posCounter goes up to 200. 5-6 secs seems a long time for me for this function. What does read_record do. What kind of computer (processor, OS) do you use?
0
 
LVL 10

Expert Comment

by:rbr
ID: 1258519
Also fseek in a non binary file could be dangerous!
0
 

Author Comment

by:checkin
ID: 1258520
OS is Solaris on a SUN Ultra 1 with 256Mb Ram

read_record is a function to return a specific field from a delimted line.  Here is it below :-

char* read_record(char *rec, int fieldNum, char delimin) {

  int a;
  char *chPtr1;
  char localrec[4096];
  char delimeter[40];
  char tmpbuf[40];

  memset(tmpbuf,'\0',sizeof(tmpbuf));
  memset(delimeter,'\0',sizeof(delimeter));
  memset(localrec,'\0',sizeof(localrec));

  sprintf(delimeter,"%c",delimin);

  for(a=0;rec[a]!='\0';a++) {
    if(a != 0) {
      if(rec[a-1]==delimin && rec[a]==delimin) {
        sprintf(localrec,"%s ",localrec);
      }
    }
    sprintf(localrec,"%s%c",localrec,rec[a]);
  }

  chPtr1 = localrec;
  chPtr1 = strtok((char*)localrec,delimeter);
  for(a=0;a<fieldNum;a++) {
    if(chPtr1+1==delimeter) { a++; }
    chPtr1 = strtok(NULL,delimeter);
  }
  if(chPtr1==NULL)
    return("NULL");
  return(chPtr1);
}

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:cpa802
ID: 1258521
I think your best bet, I dont know your situation, would be to translate the ascii file, which seems to contain only indecies back in to the same file, translate it in to a binary file.
That would make the file smaller (MUCH smaller). Then you could read the whole file in to memmory and scan through it. Then you wouldn't need to do any seeks only calculate an offset in to an array.
I know thats some what general but its the best I can do without knowing what the file contains.
If the file contains some data other than offsets back in to the same file you might want to consider an index file. Keep the data in one file with no offset information, just null terminated strings. Then have a second file, binary, that contains only offsets in to the data file. You can read the index file in to memmory and then do One seek to the data you want in the data file.
Again I dont know what the data file contains.
0
 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1258522
I think in these function most of the time is spend
remove all memset . You will not need it in this function. You only need
localrec[0]='\0';

replace
sprintf(delimeter,"%c",delimin);
by
delimeter[0]= delimin;
delimeter[1]= '\0';
sprintf is slow

sprintf(localrec,"%s ",localrec); could be replace by
strcat(localrec," ");

sprintf(localrec,"%s%c",localrec,rec[a]); replace with
strncat (localrec,&rec[a],1);

       
 
           
0
 
LVL 10

Expert Comment

by:rbr
ID: 1258523
I don't understand, why your read_record is soo difficult. Can you post the contents of inBuff and what you want to do with it. You loop everytime over the whole buffer. This can be made faster.
0
 

Author Comment

by:checkin
ID: 1258524
Basically any string that I have for example :-

field1,field2,field3,field4

I made this function so that I could pass it any delimeted string with the field number I wanted to retreive and the delimeter used.  So in the above example to retrieve field3 I would call it like this

read_record(string,2,',')

Marvin.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1258525
Change

for(i=1;i<=posCounter;i++) {
  tmpPosition = atoi(read_record(inBuff,i,','));
  fseek(idxFile,tmpPosition, 0);
  fgets(tmpStr,sizeof(tmpStr),idxFile);
}

to
tmpstr = read_record(inBuff,0,',');

for(i=1;i<=posCounter;i++) {
  tmpstr = read_record(tmpstr,0,',');
  tmpPosition = atoi(tmpstr);
  fseek(idxFile,tmpPosition, 0);
  fgets(tmpStr,sizeof(tmpStr),idxFile);
}

Why don't you use the first field?


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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and reading files in the C programming language.

758 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

19 Experts available now in Live!

Get 1:1 Help Now