Hex dump and seek(position)

I tried using hex dump with my file.txt and here is the result

Offset Hexadecimal ASCII
00000000 32 32 0D 0A 33 30 0D 0A 32 32 0D 0A 33 35 22..30..22..35

How can I apply this using file.seek(pos) of SD.h library ? how do I count and what is the total byte?

myFile = SD.open(“Ejemplo.txt”);

if (myFile) {
Serial.println(“Ejemplo.txt:”);

myFile.seek(3 * 5);
String lista = myFile.readStringUntil(’\r’);
Serial.println(lista);

myFile.txt has details:
22
40
22
30

thank you.

Why exactly do you want to use file.seek()

1 Like

what do you want to count?
the total number of bytes is the file size

seek() jumps to a specific bye position in the file. what 3x5 ??

Hi ty for your reply. I just want to understand the file.seek(pos) and by using hex dump file, how can I jump or select a line with the data coming from the hex dump?

ty for your reply. I would like to learn more about file.seek(pos). how to write the pos in the file.seek() by using the hex dump, this is to select lines in the file text

you can only seek to a position not a specific character.
the hex dump is just a difficult to read (for a mere mortal) version of the content of the file

basically the bytes 32 32 0D 0A 33 30 0D 0A 32 32 0D 0A 33 35 22 in Hex represent the following

22
30
22
35"

(OD OA being the Carriage return + Line feed to go visually to the next line in text editors)

if you want to go to the second line, after the "22" you need to skip 2 bytes for the text and then 2 other bytes for the CR and LF. so you need to skip 4 bytes, at position 0, 1, 2 and 3 in the file and position your cursor on the 5th byte ➜ you do a myFile.seek(4);

seek() is generally used for files where each record or line is always the same length. For a text file where the line length varies, you would need to read each line until you reach the desired line, keeping count of how many lines have been read.

and you can use this to seek also with variable length lines.

I had written an example for a logging function where the code maintains 2 files, one which is the actual human readable log and one that represents the position of the start of each new line.

This was used to scroll up and down efficiently in a long file, displaying 10 (or so) records at a time.

EDIT: found it, it's attached to that post (from a long thread)

download ➜ https://forum.arduino.cc/uploads/short-url/5LqvxTHyeiw8Pqnc17xy41VXcwl.ino

yes. but how would I know the correct line to choose given that every line has the same number of characters or width? Using Hex dump will it be helpful?

Ty. this would be a great reference.

Ok I will try on this. By the way to answer about 3*5, I got this while researching, they said the 3 is the 'row number' and then multiply it to 5 the ' size of the file'.

Not really; the only thing it shows is the 0x0D (carriage return) and the 0x0A (line feed). Once you know the length of one line (and all lines arev the same), you can seek

myFile.seek(N*L);

where

  • N is the line where you want to jump to (zero is the first line).
  • L is the known length of a line including the line terminators; would be 4 for your given example).

Note
I did not check the syntax of the seek function.

ok ty regarding to your info =)

Did you understand post #6?

yes thank you everyone for your help. grateful!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.