im quite new to code and are using c++ to read files. Through some examples
i have managed to read a file from top to bottom like this.
The problem is that i will need to read the file from bottom to top instead.
Is there a reversable method for this.
I know I could use vectors to reverse the information but the files are very large.
Any help would be appreciated.

StreamReader^ sr = gcnew StreamReader("TestFile.txt");
String^ line;

while (sr->Peek() >= 0)
{
	line = sr->ReadLine();
}

I've worked a little with files at school and from what I've gathered, to actually go to the end of the file you need to know how far that is from the beginning of the file either before hand or calculating it. This is not a problem with bit files but when it comes to .txt's, you just don't know how many bits will one line take, it varies.

So you might want to go through the entire file and count the number of times you encounter a line feed. and then work with that information, maybe read (n-1) lines and then read the last sentence and then go back the sizeof(last read) * -1; or something like that, just a vague idea. I actually need help with txt's too. Read this http://www.daniweb.com/forums/thread150726.html

thank you emotionalone, i wrote an idea on your question there. i have searched around a little where I found this link:
http://www.usenet-forums.com/php-language/15497-how-do-i-read-file-line-line-backwards.html

what I found here was a message
"why not just open the file, seek to the end, read byte by byte backwords, untill you find a \n then what you have read in is a line,"

As i have understand a \n tells that the end of line is reached and perheps here something can be done to save that information as a line in a string.

This will find the end of file to but from here i dont know how to continue to read.

sr->BaseStream->Seek(0, SeekOrigin::End);

I've worked a little with files at school and from what I've gathered, to actually go to the end of the file you need to know how far that is from the beginning of the file either before hand or calculating it. This is not a problem with bit files but when it comes to .txt's, you just don't know how many bits will one line take, it varies.

So you might want to go through the entire file and count the number of times you encounter a line feed. and then work with that information, maybe read (n-1) lines and then read the last sentence and then go back the sizeof(last read) * -1; or something like that, just a vague idea. I actually need help with txt's too. Read this http://www.daniweb.com/forums/thread150726.html

First, open the file in binary mode so that the os doesn't interpret the '\n' characters. Then you have to call seek() after each read operation.

What you do with '\n' depends on what operating system you are using. *nix files just have '\n', but MS-Windolws uses '\r' and '\n', and MAC uses just '\r'.

int chr;
ifstream in("filename", ios::binary);
in.seekp(-1, ios::end); // see one byte from the end
do  {
    chr = in.get();
    in.seekp(-2, ios::cur);
} while( in.tellg() > 0); // while not at beginning of file

Well, It took me around 2 hours and here's what I could come up with

#include <stdio.h>
#include <conio.h>

void main()
{
  FILE *pS, *pD, *pT;
  int a, n=0;
  char c='@';
  char *dabuff;

  pS = fopen("ORIGINAL.TXT","rt");
  pD = fopen("BACKWARDS.TXT","wt");
  fseek(pS,sizeof(char)*-1,SEEK_END);

  c = fgetc(pS);
  a = c;
  n++;

  while( a!=-1 )
  {
    fseek(pS,sizeof(char)*-2,SEEK_CUR);
    c = fgetc(pS);
    a = c;
    if( c == 10 || c == -1)
    {
      if( c == -1)
        fseek(pS,0,SEEK_SET);
      for(int i=n;i>0;i--)
      {
        c=fgetc(pS);
        fputc(c,pD);
      }
      fputc(10,pD);
      fseek(pS,sizeof(char)*-(n+1),SEEK_CUR);
      n=0;
      continue;
    }
    n++;
 }
 fcloseall();
}

It's not flawless, but it's about right. Still encountering some issues I don't yet know how to solve.

Your program seems to be too complex.

int main()
{
  FILE *pS, *pD, *pT;
  int a, n=0;
  int[/color  c=0;
  char *dabuff;

  pS = fopen("INVITACION.TXT","rb");
  pD = fopen("BACKWARDS.TXT","wt");
//
// no need to use sizeof(char) because its guarenteed to be 1 by the standards
//
  fseek(pS,-1,SEEK_END);
  while( ftell(pS) > 0)
  {
     c = fgetc(pS);
     if( c != '\r' ) // you don't need this for *nix or MAC
         fputc(c,pD);
     fseek(pS,-2,SEEK_CUR); 
  }

Yes, that gives you a character by character backward .txt output file.
But if you want

1122334455
AABBCCDD
##$$%%^

to be delivered as

##$$%%^
AABBCCDD
1122334455

instead of

^%%$$##
DDCCBBAA
5544332211

I thought you would first need to know how many chars to read first, then go back that same ammount of bits + 2 for the carriage return and line feed

I think I got something out of this and trying around now with different testings.
Thank you for your effort and help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.