Problem with fseek , seems to be an overlap anywhere ..

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with fseek , seems to be an overlap anywhere ..

891 Views
monXii
Contributor III

Hello all.

 

I have a problem with fseek !

 

I have a big (490k) text file on my sd card.

 

It's a "personal database" .. build like:

 

%5d (personal no.)

";"

%20s (first name)

";"

%20s (last name)

";"

"\n"

 

In this text file are 10000 entries like this format ..

 

Now i want to search in this text file personal no "00084" ..

 

My first run was with getline() ..

Works fine, but is too slow!

 

Then i changed my search function to work with fseek .. so i dont have to read the whole line ..

 

but when pnr_fp->LOCATION is bigger than 4096, the read() reads from (LOCATION - 4096) .. ?!

for example pnr_fp->LOCATION is 0x1014 .. he should read @ file offset 4116 ..

but he reads @ file offset 20 ..

 

someone knows this problem? is there a workaround? is there anywhere a define which i can modify?

 

 

 

here is the code snippet ..

 

#define SEARCH_WITH_GETLINE 0

 

boolean pnr_suche_pnr (char_ptr pnr) {
    MQX_FILE_PTR    pnr_fp=NULL;
    _mqx_int        bytes_read=0;
    char            pnr_buffer[50];
    uint_32            line=0;
    uint_8            tmp_blocker=100;
    
    pnr_fp=fopen("a:smileytongue:nr.txt", "r+");
    
    if (pnr_fp==NULL) { return FALSE; }

    do {
        if (--tmp_blocker==0) { return FALSE; }
        
        printf("file_location b r: %x\n", pnr_fp->LOCATION);
        
#if (SEARCH_WITH_GETLINE==1)
        bytes_read=fgetline(pnr_fp, pnr_buffer, 50);
#else
        bytes_read=mfs_file_read(pnr_fp, pnr_buffer, 7);
        pnr_buffer[7]=0;
#endif
        
        printf("file_location a r: %x\n", pnr_fp->LOCATION);
        
        if (bytes_read>0) {
            printf("pnr: <%s>\n", pnr_buffer);
            
            // check personal no.
            if (strncmp(pnr, pnr_buffer, 5)==0) {
                fclose(pnr_fp);
                return TRUE;
            }
        }

#if (SEARCH_WITH_GETLINE==0)
        if (fseek(pnr_fp, (line++ * 49), IO_SEEK_SET)!=MQX_OK) {
            fclose(pnr_fp);
            printf("eof? location: %x\n", pnr_fp->LOCATION);
            return FALSE;
        }
#endif
    } while (bytes_read>0);
    
    fclose(pnr_fp);
    
    return FALSE;
}

 

 

Labels (1)
Tags (1)
0 Kudos
2 Replies

439 Views
Cdn_aye
Senior Contributor I

Hi

 

This may not apply to your problem but I have found there are issues with the MFS and USB. The fflush does not work as other file systems I have used, for example in a DOS system the fflush moves EOF marker to the EOF. What I have verified and so has support in fact nothing happens unless you close and reopen the file. This is therefore a waste of time; the purpose of fflush is to protect the file incase of a device failure and remove the need to traverse the whole file block chain on an open. But it does nothing regarding the file operation so the file has to be closed and opened for every write; burning up a lot of time.

 

But more to the point of your system I have seen the delete and write functions fail as well. This on my system, was a problem with stack overflow due to the RAM size on the processor I was running. I used the 51jm128 and to correct this used the TAD to carefully tune the stack. Then the last thing was to increase the priority of the task running the file routines above the USB monitoring routine. Then it started to work. I still have a problem where after the file gets a certain size the file open fails and never again succeeds;; ie returns a null pointer. That one I am still trying to work out.

439 Views
monXii
Contributor III

Hey Robert.

 

Thanks for your reply!

 

I increased my stack from 2000 up to 8000 Bytes.. :smileyhappy:

 

My main_task, were the shell runs and my function is called from, has Priority 7 and the sdcard_task has Priority 9 ..

 

Same problem ..

 

Maybe here are some more informations ..

 

Using:

- K60 (so there should be enough RAM :smileywink: .. )

- MQX 3.8

- CW10.1

0 Kudos