problem reading a in reverse order

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 3
Reputation: srisaravanavel is an unknown quantity at this point 
Solved Threads: 0
srisaravanavel srisaravanavel is offline Offline
Newbie Poster

problem reading a in reverse order

 
0
  #1
Jul 26th, 2008
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.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
   char data[10000];

   int i=0;

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

  // clrscr();
   
   ofile.seekg(-1,ios::end);

   while(1)

   {

    if(ofile.tellg()==-1)
    
break;
  
    i++;
    ofile.get(data[i]);


    ofile.seekg(-2,ios::cur);



  }


  data[i]='\0';

  cout<<data;


getch();

}//main
Last edited by Tekmaven; Jul 26th, 2008 at 4:39 am. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: problem reading a in reverse order

 
0
  #2
Jul 26th, 2008
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?
Last edited by iamthwee; Jul 26th, 2008 at 7:23 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: srisaravanavel is an unknown quantity at this point 
Solved Threads: 0
srisaravanavel srisaravanavel is offline Offline
Newbie Poster

Re: problem reading a in reverse order

 
0
  #3
Jul 26th, 2008
Print the entire file in reverse. not line by line.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: problem reading a in reverse order

 
0
  #4
Jul 26th, 2008
test.txt

  1. I am God yes I am
  2. I know I am
  3. dam right!

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <climits>
  6.  
  7. using namespace std;
  8.  
  9. class Foo
  10. {
  11. public:
  12. void reverse ( string t )
  13. {
  14. int s;
  15. s = t.length();
  16.  
  17. for ( int i = s - 1; i >= 0; i-- )
  18. {
  19. cout << t[i];
  20. }
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. ifstream read ( "test.txt" );
  27. string line;
  28.  
  29. vector <string> stuff;
  30.  
  31. while ( getline ( read, line, '\n' ) )
  32. {
  33. stuff.push_back ( line );
  34. }
  35. read.close();
  36.  
  37. int t;
  38. t = stuff.size();
  39.  
  40. for ( int i = t - 1; i >= 0; i-- )
  41. {
  42. Foo test;
  43. test.reverse ( stuff[i] );
  44. cout << "\n";
  45. }
  46. cin.get();
  47. }

My output
  1. !thgir mad
  2. ma I wonk I
  3. 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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: problem reading a in reverse order

 
0
  #5
Jul 26th, 2008
you can reverse using std::reverse :}
std::reverse(string.begin(), string.end());
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: problem reading a in reverse order

 
0
  #6
Jul 27th, 2008
> 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'
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: srisaravanavel is an unknown quantity at this point 
Solved Threads: 0
srisaravanavel srisaravanavel is offline Offline
Newbie Poster

Re: problem reading a in reverse order

 
0
  #7
Jul 29th, 2008
Thank you for all of your solutions.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC