Aug 21, 2008 at 2:26pm UTC
#include <iostream>
#include <fstream>
main()
{
ifstream a;
int z,n;
char ch;
a.open("dta.txt");
a.get(ch);
n=1;
while (!a.eof()){
while (!a.eof()&& (ch!='\n'))
{
cout << n << ch << endl;
n++;
a.get(ch);
}
}
a.close();
cin >> z;
}
this program is supposed to copy this file and display it on my monitor.Although it works but it displays it character by character i want to display it line by line.Help me pls!Thanks anyway
Aug 21, 2008 at 4:06pm UTC
i need to announce 2 new libraries,one for string = <string>
and one for getline which i dont know,....
Aug 21, 2008 at 4:48pm UTC
getline() is also defined in <string> .
Aug 21, 2008 at 5:34pm UTC
it doesnt work thanks anyway
Aug 21, 2008 at 6:22pm UTC
How does it not work? The string getline() function is designed exactly to do what you want.
Here is a simple
cat /
more type program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void pressenter( const string& prompt )
{
cout << prompt;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
}
int main( int argc, char ** argv )
{
vector <string> args( argv, argv +argc );
if (args.size() < 2)
{
cout << "usage:\n "
<< args[ 0 ] << " [LINES] FILENAME ...\n\n"
<< "where LINES defaults to 24 lines per screenful\n"
<< "specify 0 lines to display the entire file without pause\n" ;
return 0;
}
// Get the maximum number of lines to display on the screen at once
unsigned lines_per_screen = 24;
if (stringstream( args[ 1 ] ) >> lines_per_screen)
args.erase( args.begin() +1 );
if (lines_per_screen == 0) lines_per_screen--;
// For each filename listed...
for (vector <string> ::const_iterator
arg = args.begin() +1;
arg != args.end();
arg++
) {
ifstream f( arg->c_str() );
if (!f)
{
cerr << "Could not open file \"" << *arg << "\"\n" ;
continue ;
}
// Display the lines of the file, lines_per_screen at a time
unsigned line_number = 0;
string line;
while (getline( f, line ))
{
if (line_number++ == lines_per_screen)
{
pressenter( "Press ENTER to continue" );
line_number = 1;
}
cout << line << endl;
}
f.close();
if (lines_per_screen != (unsigned )(-1))
{
cout << string( lines_per_screen -line_number, '\n' );
pressenter( "End of file. Press ENTER for next" );
}
}
return 0;
}
Hope this helps.
Last edited on Aug 21, 2008 at 6:23pm UTC