The below program works fine for a input file that has only one line. but goes to an infinite loop for a multiline file. Kindly suggest.


void main()
   char data[10000];

   int i=0;

   ifstream ofile("c:\\temp\\welcome.txt");
   cout<<"File opened\n";

  // clrscr();










What are you trying to do, read each line in reverse order, or take a line and read the characters from right to left or what?


I am God yes I am
I know I am
dam right!
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

class Foo
  void reverse ( string t )
    int s;
    s = t.length();

    for ( int i = s - 1; i >= 0; i-- )
      cout << t[i];

int main()
  ifstream read ( "test.txt" );
  string line;

  vector <string> stuff;

  while ( getline ( read, line, '\n' ) )
    stuff.push_back ( line );

  int t;
  t = stuff.size();

  for ( int i = t - 1; i >= 0; i-- )
    Foo test;
    test.reverse ( stuff[i] );
    cout << "\n";

My output

!thgir mad
ma I wonk I
ma I sey doG ma I

But I doubt that would work for you cos you're probably using turbo c 3.0 or something, in which case I would pretend you are writing a c program and use char arrays.

> but goes to an infinite loop for a multiline file. Kindly suggest.
Because you only count bytes, not characters.

> ofile.seekg(-2,ios::cur);
By opening the file in text mode, you make it so "\r\n" is translated to "\n" when you read the file. So you get stuck in a "forward 2, back 2" loop.

For text files, it's best to read them line by line, then do whatever it is you need to do.

'seek'ing through text files is possible, but you can only seek to
- the beginning of the file
- the end of the file
- the result of a previous 'tell'

This article has been dead for over six months. Start a new discussion instead.