| | |
problem reading a in reverse order
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Solved Threads: 0
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
test.txt
My output
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.
C++ Syntax (Toggle Plain Text)
I am God yes I am I know I am dam right!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> #include <vector> #include <climits> using namespace std; class Foo { public: 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 ); } read.close(); int t; t = stuff.size(); for ( int i = t - 1; i >= 0; i-- ) { Foo test; test.reverse ( stuff[i] ); cout << "\n"; } cin.get(); }
My output
C++ Syntax (Toggle Plain Text)
!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.
*Voted best profile in the world*
> 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'
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'
![]() |
Similar Threads
- A few questions to finish up my project (Pascal and Delphi)
- data sorts in reverse order!! help!! (C)
Other Threads in the C++ Forum
- Previous Thread: GUI: 3 C:\Dev-Cpp\lolLOLlol.cpp expected `,' or `;' before numeric constant
- Next Thread: Why constructor can not be defined as virtual
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






