#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
usingnamespace std;
int main(int argc, char *argv[])
{
int c; // input character
int i; // loop counter
char filename[MAX_PATH+1];
char input_line[MAX_PATH+1];
if (argc > 1)
strncpy(filename, argv[1], MAX_PATH);
else {
cout << "Enter file name: ";
cin.getline(filename, MAX_PATH);
}
ifstream file_in(filename);
if (!file_in) {
cout << filename << " could not be opened.";
return -1;
}
while (true) {
for (i = 1; i <= 24 && !file_in.eof(); i++) {
file_in.getline(input_line, MAX_PATH);
cout << input_line << endl;
}
if (file_in.eof())
break;
cout << endl;
cout << "More? (Press 'Q' & ENTER to quit.)";
cin.getline(input_line, MAX_PATH);
c = input_line[0];
if (c == 'Q' || c == 'q')
break;
}
return 0;
}
But it doesn't display the text file correctly, I get about three lines and then blank screens after pressing a key to continue reading the file. I didn't write this, it's from a book and as far as I can tell I have entered it correctly (although I added the "#include <windows.h>" so I could use MAX_PATH).
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
int main( int argc, char* argv[] )
{
// why use a magic number when it is so easy to give it a name?
// see: http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constantsenum { PAGE_SIZE = 24 } ;
// prefer std::string over c-style char arrays
// particularly when the size is not known at compile-time
// see: http://www.cprogramming.com/tutorial/string.html
std::string file_name ;
if( argc > 1 ) file_name = argv[1] ;
else
{
std::cout << "Enter file name: " ;
std::cin >> file_name ;
// after this input, some cruft will be left in the buffer; remove it
// we want the inuput buffer to be empty just before the user
// types something in the next time.
// see: http://www.cplusplus.com/reference/iostream/istream/ignore/
// std::numeric_limits<std::streamoff>::max() gives the largest possible
// value for the number of chars that can be in a stream buffer
// see: http://en.cppreference.com/w/cpp/types/numeric_limits/max
std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ;
}
std::ifstream file_in( file_name ) ; // file_in( file_name.c_str() ) is required in old C++
if( !file_in )
{
// it is a good idea to send error messages to stderr
// see: http://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29
std::cerr << file_name << " could not be opened.\n" ;
return -1;
}
std::string line ;
// we need to check for eof after (and not before) reading each line
for( int i = 1 ; std::getline( file_in, line ) ; ++i )
{
// if a line has been successfully read
std::cout << line << '\n' ; // print out the line
if( i%PAGE_SIZE == 0 ) // if a complete page has been printed
{
std::cout << "More? (Press 'Q' & ENTER to quit.)" ;
// we just need the first char typed in by the user
char c = std::cin.get() ;
if( c == 'Q' || c == 'q' ) return 0 ;
else std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ;
}
}
// return 0 is assumed here
}
I don't really understand what's happening in the parentheses of cin.ignore(??!). What exactly is the difference between the full statement and a simple "cin.ingore()" with no parameters?
std::cin.ingore() will just ignore a single char. std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ; ignores std::numeric_limits<std::streamoff>::max() characters.
You don't specify the delimiter character so it will not stop until all the std::numeric_limits<std::streamoff>::max() characters has been entered from the user or EOF is reached. If you are trying to ignore only the rest of the line you should write std::cin.ignore( std::numeric_limits<std::streamoff>::max(), '\n') ;
include <iostream>
#include <fstream>
#include <string>
#include <limits>
int main( int argc, char* argv[] )
{
// why use a magic number when it is so easy to give it a name?
// see: http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constantsenum { PAGE_SIZE = 24 } ;
// prefer std::string over c-style char arrays
// particularly when the size is not known at compile-time
// see: http://www.cprogramming.com/tutorial/string.html
std::string file_name ;
if( argc > 1 ) file_name = argv[1] ;
else
{
std::cout << "Enter file name: " ;
std::cin >> file_name ;
// after this input, some cruft will be left in the buffer; remove it
// we want the inuput buffer to be empty just before the user
// types something in the next time.
// see: http://www.cplusplus.com/reference/iostream/istream/ignore/
// std::numeric_limits<std::streamoff>::max() gives the largest possible
// value for the number of chars that can be in a stream buffer
// see: http://en.cppreference.com/w/cpp/types/numeric_limits/max
std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ;
}
std::ifstream file_in( file_name ) ; // file_in( file_name.c_str() ) is required in old C++
if( !file_in )
{
// it is a good idea to send error messages to stderr
// see: http://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29
std::cerr << file_name << " could not be opened.\n" ;
return -1;
}
std::string line ;
// we need to check for eof after (and not before) reading each line
for( int i = 1 ; std::getline( file_in, line ) ; ++i )
{
// if a line has been successfully read
std::cout << line << '\n' ; // print out the line
if( i%PAGE_SIZE == 0 ) // if a complete page has been printed
{
std::cout << "More? (Press 'Q' & ENTER to quit.)" ;
// we just need the first char typed in by the user
char c = std::cin.get() ;
if( c == 'Q' || c == 'q' ) return 0 ;
else std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ;
}
}
// return 0 is assumed here
}
Here is the error
error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : can
not convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.