element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum How can I read from a SD card file backwards?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 6 replies
  • Subscribers 393 subscribers
  • Views 2155 views
  • Users 0 members are here
  • write
  • sd_card
  • read
  • sdcard
Related

How can I read from a SD card file backwards?

Former Member
Former Member over 10 years ago

Currently working on a time of day project.

I have a file on my Arduino's SD card called mail.txt. There is a push button hooked up to the arduino with an Ethernet shield.

 

Action that occurs. You push the button. Arduino looks at the RTC. And writes a new line to mail.txt the time and date the button was pushed. Very simple. That part works.

 

When you go to the webpage hosted by the Arduino. You will see the data log from the mail.txt.....

 

2013-01-12 MON 15:23
2013-01-13 TUE 14:26
2013-01-14 WED 11:13
2013-01-15 THU 16:12
2015-02-05 THU 17:48
2015-02-05 THU 17:55
2015-02-05 THU 17:58
2015-02-05 THU 18:03

 

 

My question to the forum, how can I have this show up in reverse order (Recent on top)? Either Write the new button push to the top line and push everything else down, OR have the arduino read the mail.txt file from bottom line to the top line.

 

I have dug through tons of code, and search results, all about myFile.write and myFile.read. But nothing about the direction to read it from. Is there a way to perhaps count the amount of Line breaks in the mail.txt file. Then Go back and read read the mail.txt file and grab the last one. Then repeat it again, with a -1 and write out the second to last one? 

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 10 years ago +1
    John Unless you read in the entire file and then rewrite it each time (risky and too much memory) you are stuck with writing to the end of the file (appending). You can read the last line then come back…
Parents
  • mcb1
    0 mcb1 over 10 years ago

    John

    Unless you read in the entire file and then rewrite it each time (risky and too much memory) you are stuck with writing to the end of the file (appending).

    You can read the last line then come back and read the previous line.

    If the amount of chars is fixed or the seperator is always constant, then its possible.

     

    Apart from the asthetics, does it really matter that its last in at the bottom.?

     

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to mcb1

    I am a bit confused with your assumptions Clem.

     

    1) Yes it will always be a fixed character string

    2015-02-05 THU 18:03 </br>

    2) The byte count is 28. (per write) . I am guessing you are suggesting (Which a light bulb is slowing getting brighter on the top of my head). That at the end of the Write process. I check to see what the mailLog.size() is. Then do a second loop to seek -28 from the total. And then have it loop 28 times. Then back up another -28.

     

    So for example. Button is pushed, it writes the date stamp the file. Then before close. It finds out the file size is 282 bites. I would then run a loop to take the total, subtract 28 from it. Then search the maillog.txt file till you get to byte 254. And read the next 28 characters. Then subtract 28 again. Getting it to start at 226. And read those 28 character?

     

    3) No, I have no intentions. When button is pushed is the only time it is writing to the log file. And only is read from the log file when a http request is activated. (Aka me visiting the webpage)

     

    My question is how do I go about opening the file as 'direct access'? Or is this the same as the normal myFile.read()?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 10 years ago in reply to mcb1

    I am a bit confused with your assumptions Clem.

     

    1) Yes it will always be a fixed character string

    2015-02-05 THU 18:03 </br>

    2) The byte count is 28. (per write) . I am guessing you are suggesting (Which a light bulb is slowing getting brighter on the top of my head). That at the end of the Write process. I check to see what the mailLog.size() is. Then do a second loop to seek -28 from the total. And then have it loop 28 times. Then back up another -28.

     

    So for example. Button is pushed, it writes the date stamp the file. Then before close. It finds out the file size is 282 bites. I would then run a loop to take the total, subtract 28 from it. Then search the maillog.txt file till you get to byte 254. And read the next 28 characters. Then subtract 28 again. Getting it to start at 226. And read those 28 character?

     

    3) No, I have no intentions. When button is pushed is the only time it is writing to the log file. And only is read from the log file when a http request is activated. (Aka me visiting the webpage)

     

    My question is how do I go about opening the file as 'direct access'? Or is this the same as the normal myFile.read()?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    Good answer for #1 statement. Then #2 would be the logic you need. My assumption #3 is satisfied by your #3. Here is a good tutorial on reading Direct/random access versus sequentially in a file. Make sure to count any NL or other bytes as well. "</br>" maybe one or two at the most. A good hex viewer can show nicely.

     

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to clem57

    I read the articular and am completely confused now. The articular itself sorta makes seance on its own. But converting it to arduino language is where I am stumped. I am trying to start off with basic actions, and am hitting road blocks.

     

    As of right now. When the button is pushed, it writes to the end of maillog.txt as described before. After the write, it notes the file size as 'myFilesSize' After that myFile.close() has occurred. I then go back and read the file.

     

    Which brings me here:

    ---------------

      char previous1[28] = ("1990-01-23 SAT 12:34 </br>");   // newest-youngest time stamp

      char previous2[28] = ("1990-01-23 SAT 12:34 </br>");

      char previous3[28] = ("1990-01-23 SAT 12:34 </br>");

      char previous4[28] = ("1990-01-23 SAT 12:34 </br>");

      char previous5[28] = ("1990-01-23 SAT 12:34 </br>");   // oldest time stamp

      char prev_index = 0;

     

      mailFileSize = mailFileSize - 139;               // Subtracts back to 5 lines (To get the starting position.

     

      // knowing the file size from the integer. And knowing each line is = 28 bytes. x previous 5 lines.

      // 28 x 5 == 140 bytes back.

      myFile = SD.open("maillog.txt");

      myFile.seek(mailFileSize);                       // Goes to that location

     

     

      while (myFile.position() < 28) {

        Serial.print(myFile.read());                 // Reads the character. Spits it out as a Decimal number

        Serial.print(" ");                           // example: 50 48 49 53 45 48 50 45 48 57 32 83 65 84 32 49 50 58 53 49 32 60 47 98 114 62 13 10

     

        //previous5[prev_index] = myFile.read();

        prev_index++;                                // increases the index value. Since it started at 0. This put

     

      }

      Serial.println("end");

      Serial.println(previous5);

      myFile.close();

     

    ---------------

     

    My plan. To open up the maillog.txt file. Got to 5 lines from the end (140 bytes from the end) Read the next 28 bytes which is the whole 'line' of text (1990-01-23 SAT 12:34 </br> [carriage return] [New line feed])  During the loop, this is written to a char previous5[] variable. That I will then use later to write to a recent.txt file. (I will order from most newest to oldest. This is sorted by the previous# char's) Which is what I will do later once I can get the arduino to correctly create the previous# variables.

     

     

    Here are the issues.

     

    1) myFile.seek(mailFileSize)  does not work. Say the file size is 420. Due to the math, That would place the cursor at byte 281. Even if I put the number 281 in the parthresees. It does not relocate to byte 281. Am I not using the myFile.seek correctly?

     

    2) The while works. It will Serial.print 28 bytes in the form of DECIMAL characters. as shown in the // example in the code. My issue is building the previous5[] variable. If I un comment it. The Serial.print will become shortend to:  50 49 45 50 48 32 65 32 50 53 32 47 114 end

    And the previous5[] Variable will Serial.println will spit out:  040-9ST1:1<b>T 12:12 </br>

     

     

    2.1) Is there a way to get The first Serial.print(myFile.read()) to print the ASCII characters instead of Decimals?

     

    2.2) Should I be formating the previous5[prev_index] = myFile.read(); differently? So that it too is creating the variable as Ascii characters instead of Decimals?

     

    3) Overall question. Am I formating the char variable correcty when I define them?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube