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 2157 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
  • clem57
    0 clem57 over 10 years ago

    Assumptions:

    1. Fix number of characters written each time.
    2. Count of characters written is updated because the file is flushed/closed after each write
    3. No race conditions of writing and reading concurrently

    If so, open the file as direct access and calculate the number of bytes in file now. Subtract the fix characters written to read first record. Repeat loop backwards until first record at byte 0.

     

    Clem

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

    Assumptions:

    1. Fix number of characters written each time.
    2. Count of characters written is updated because the file is flushed/closed after each write
    3. No race conditions of writing and reading concurrently

    If so, open the file as direct access and calculate the number of bytes in file now. Subtract the fix characters written to read first record. Repeat loop backwards until first record at byte 0.

     

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
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