Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:17:54+00:00 2026-05-24T06:17:54+00:00

file looks like this: abcd efgh ijkl I want to read the file using

  • 0

file looks like this:

abcd
efgh
ijkl

I want to read the file using C so that it read the last line first:

ijkl
efgh
abcd

I cannot seem to find a solution that does not use an array for storage. Please help.

edit0:
Thanks for all the answers. Just to let you know, I am the one creating this file. So, can I create in a way its in the reverse order? Is that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:17:55+00:00Added an answer on May 24, 2026 at 6:17 am

    It goes like this:

    1. Seek to one byte before the end of the file using fseek. There’s no guarantee that the last line will have an EOL so the last byte doesn’t really matter.
    2. Read one byte using fgetc.
    3. If that byte is an EOL then the last line is a single empty line and you have it.
    4. Use fseek again to go backwards two bytes and check that byte with fgetc.
    5. Repeat the above until you find an EOL. When you have an EOL, the file pointer will be at the beginning of the next (from the end) line.
    6. …
    7. Profit.

    Basically you have to keep doing (4) and (5) while keeping track of where you were when you found the beginning of a line so that you can seek back there before starting your scan for the beginning of the next line.

    As long as you open your file in text mode you shouldn’t have have to worry about multibyte EOLs on Windows (thanks for the reminder Mr. Lutz).

    If you happen to be given a non-seekable input (such as a pipe), then you’re out of luck unless you want to dump your input to a temporary file first.

    So you can do it but it is rather ugly.

    You could do pretty much the same thing using mmap and a pointer if you have mmap available and the "file" you’re working with is mappable. The technique would be pretty much the same: start at the end and go backwards to find the end of the previous line.


    Re: "I am the one creating this file. So, can I create in a way its in the reverse order? Is that possible?"

    You’ll run into the same sorts of problems but they’ll be worse. Files in C are inherently sequential lists of bytes that start at the beginning and go to the end; you’re trying to work against this fundamental property and going against the fundamentals is never fun.

    Do you really need your data in a plain text file? Maybe you need text/plain as the final output but all the way through? You could store the data in an indexed binary file (possibly even an SQLite database) and then you’d only have to worry about keeping (or windowing) the index in memory and that’s unlikely to be a problem (and if it is, use a "real" database); then, when you have all your lines, just reverse the index and away you go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider my source file looks like this. <Content xmlns=uuid:4522eb85-0a47-45f9-8e2b-1x82c78xx920> <first>Hello World.This is Fisrt field</first>
I am uploading image using filedrop.js. Code in application.js file looks like this:- $(.droparea).each(function(event){
I have a data file that looks like this: xyz123 2.000 -0.3974 0.0 hij123
My first CSV file looks like this with header included (header is included only
I have a RTB that is loaded with a file that looks like this:
I am using XML::LibXML (Ver: 1.70). My xml input file looks like this: <?xml
The Date on the second line of the file looks like this 12-24-11 13:37
My application.js file looks like this: //= require jquery //= require jquery_ujs //= require
My config file looks like this: title = myTitle; otherTitle = myOtherTitle; when I
I have an XML file looks like this: <?xml version=1.0 encoding=utf-8 ?> <PathMasks> <Mask

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.