How to find the line number from arduino txt file?

Hi

I having text file having more than 8000 lines of data. I want to read particular line from that text file like 5023rd line or 6000th line. Is there any possible to find that using arduino default SD library.

Please suggest some solution.

Thanks in Advance.

Is the data on each line fixed or variable length ?

If it is fixed length then look at the seek() function in the library.

all lines are different length, not fixed length.

Start counting newline('\n') and/or carriage ('\r') characters; it depends a little on how the file was created (Windows, Mac, Linux). You need to read the file character by character to find them.

// edit
If it's possible, you can create a second file that stores the position of each line; if you're looking for a specific line, read this second file, get the position and use seek to navigate in the text file.

Actually i tried with that but its take more delay to find the line. Then i have to find the line multiple time in programming. It will be take more time.

Is there any other way?? Please suggest.

Thank you.

Yes, it will take more time when you have to read character by character. Did you see my edit? You might have been posting while I did the edit.

You can also keep a partial index in memory. E.g. the start position of every 100th line; in that case you find the closest (lower) index and only have to read a max of 100 lines character by character.

Can you provide an example of typical data (e.g. 10 lines)?

sterretje:
Yes, it will take more time when you have to read character by character. Did you see my edit? You might have been posting while I did the edit.

You can also keep a partial index in memory. E.g. the start position of every 100th line; in that case you find the closest (lower) index and only have to read a max of 100 lines character by character.

Can you provide an example of typical data (e.g. 10 lines)?

data in text file:

00AAAAAA555555
00BBB22222
00CCCCCCCCCCCCCAAAAAAAAAAAAAAAAAA
005555555555555555555AAAAAAAAAA
00AAAAAAAAAAACCCCCCCCCCCCCCCCCCCC
00AAAA22222222222222222333333333333333
006600555000555550005555000555
00ABABABABABABABBABABBABAB
0055AA55AA55AA55AA55AA55AA55AA55AA55AA55AA
00123456789ABCDEF123456789ABCDEF
00FEDCBA98765432100321654987FEDABC
00AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

//like this data having more than 6000 lines. I need find the number of lines with in a second.

Is there any other way??

No. You have to read every byte looking for the new lines, or you have to "know" where the line breaks are, either by storing them separately or by having fixed-length lines. There used to be a bunch of ways of optimizing this sort of thing, back when computers and disks were slow and memories and disks were small, but they all amount to storing index information separately. pre-processing the file on a PC to either produce the indexing info (which COULD go in the beginning of the same actual file, rather than a separate file), or changing the lines to fixed-length, should be relatively easy. And the "space waste" ought to be insignificant for a ~10000 line file on a 4G+ SD card...

As prev poster mentioned, the best strategy would be to index the lines as soon as you install a new card - and update that if you append any new data.

The penalty will be pretty small - a couple of seconds when a new card is installed, and virtually instantaneous from then on.

Make sure to.plan your memory use and sizing before you start coding!

harishree7:
all lines are different length, not fixed length.

Can you pre-process the file and pad the lines to make a fixed length version?