Solved

Seeking Positions in a File

Posted on 1999-01-29
10
295 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
[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
  • 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
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.

 
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
 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

623 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