Jump to content

Read a File Backwards

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Paradine

Paradine

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
How can I read a file backwards and copy it?


Example:


file 1:

a
b
c



file 2:

c
b
a


#2
cp2o

cp2o

    Newbie

  • Members
  • Pip
  • 3 posts
You could copy the file contents to a buffer then output them to another file backwards at your leisure?

Or, (assuming you're using C) you could look into the lseek() function which repositions the file offset - start at the end of the file, read a character, decrement the offset with lseek, repeat; then ouput the buffer to a new file?

I'm just a newbie, but those are my suggestions.
May the source be with you... always

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I agree. If you are splitting the file by line breaks (which it looks like you are) you can just read the array backwards as you write.

#4
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
You could always byte read it backwards.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For C++, you could create a stack of strings, push strings on the stack as you read, then pop them to write the output.

What size files are you looking at working with?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog