TXT file reader: I can't see the problem

A very small simple program to display a .txt file 24 lines at a time:

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
using namespace 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).

Thanks
Here is another very small simple program to display a .txt file 24 lines at a time:

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
#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_constants
    enum { 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
}

Thanks JL, very enlightening.
I have a quick question about this statement:

std::cin.ignore( std::numeric_limits<std::streamoff>::max() ) ;

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?
Last edited on
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') ;
I tryed to get this code to work, but I can't.
I am using VC++6.

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
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_constants
    enum { 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.

Text1.exe - 1 error(s), 0 warning(s)
Last edited on
Topic archived. No new replies allowed.