Thread: "file_in.getline", compiler error?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    40

    "file_in.getline", compiler error?

    Hey guys, thanks in advance for the help.

    I'm fairly new to C++, so the code should look very simple to the guru crowd. When I try to compile the short program below, I get the error that "struct std:: ofstream has no member function 'getline'". I've checked around a bit and this line is used fine elsewhere, as well as in the book I'm learning from. Here's the code, with the problem area bolded:
    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    int main() {
        int c,i;
        char filename[81], input_line[101];
    
        cout << "Enter a filename and press enter: ";          //prompt for filename
        cin.getline(filename, 80);
        ofstream file_in(filename);
    
        if (!file_in) {                                            //avoiding errors
            cout << "File " << filename << "could not be opened.";
            return -1;
        }
    
        while (1) {                                               //prints five lines of the selected file at a time
            for (i = 1; i < 5 && !file_in.eof(); i++) {
                file_in.getline(input_line, 100);
                cout << input_line << "\n";
            }
    
            if (file_in.eof()) {                                  //exits if end of file is reached
                break;
            }
    
            cout << "More? (Press 'Q' and ENTER to quit): "; //ask user if they want more printed
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q') {
                break;
            }
        }
    
        cin.get();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You want to read from file_in -> make it an ifstream.
    Kurt

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you use std::getline instead of ifstream.getline and use std::string instead of char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Heh, I knew it would be a small stupid error like that. Thanks.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also see you're using eof() as condition in the header for the loop. That's also bad. For details, you can see the FAQ. It's better to check if the read succeeds or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Thanks Elysia. As I was reading over the code a last time before posting, I thought of that line in your sig when I went over the eof() part, heh.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    The original problem referred to by this thread was fixed, and since then a new problem arose.

    Going along with my book, one of the exercises is to make the code that I posted in the first post print the designated file in all lowercase letters (the file reads AAAA\n BBBB\n CCCC\n etc). The problem code that I have now compiles fine, but when run, it ignores the requested number of lines to be printed and instead prints one line every time (the change from capital to lowercase works fine). Here's the code:
    Code:
    #include<iostream>
    #include<fstream>
    #include<stdlib.h>
    #include<ctype.h>
    using namespace std;
    
    int main() {
        int c, i, lines;
        char filename[81], input_line[101];
    
        cout << "Enter a filename and press enter: ";
        cin.getline(filename, 80);
        ifstream file_in(filename);
    
        if (!file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
    
            cout << "Enter number of lines to display, ";
            cout << "or press 'Q' and ENTER to quit: ";
            cin.getline(input_line, 80);
            lines = atoi(input_line);
            c = input_line[0];
            if (c == 'Q' || c == 'q') {
                break;
            }
    
            for (i = 0; i < lines; i++) {
                file_in.getline(input_line, 100);
    
                for (i = 0; i < (strlen(input_line)); i++) {
                    input_line[i] = tolower(input_line[i]);
                }
    
                cout << input_line << endl;
            }
    
            if (file_in.eof()) {
                cout << "The end of the file has been reached.";
                break;
            }
        }
    
        cin.get();
        return 0;
    }
    Thanks in advance.
    Last edited by PAragonxd; 02-02-2008 at 03:11 PM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
        for (i = 0; i < lines; i++) {
            file_in.getline(input_line, 100);
    
            for (i = 0; i < (strlen(input_line)); i++) {
    Wonder what messes up loop counter?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Slap me hard. Please.

    Thanks lol..

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May I also point out that strlen is a bit expensive function to call a lot (it has to loop over the entire array to determine the length). In fact you needn't find out the length at all and simply loop converting characters to lowercase until you get to the null-terminator of input-line.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dare I say it will be optimized by the compiler in release mode?
    Then again, maybe not. I suppose the compiler cannot assume that strlen returns the same value each time.
    But anyway, unless time is critical, it's not really necessary. So we could just say it's good practice.
    Last edited by Elysia; 02-02-2008 at 05:43 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    Just for reference, how would I go about making it so strlen is only called once (seeing as the line of text that strlen is measuring is called within the 'for' loop) ?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Store it in a variable and use the variable in the loop condition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Dare I say it will be optimized by the compiler in release mode?
    I just tried it with gcc 4.1 some time ago.

    It only gets optimized on -O3, not even -O2.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To convert every character in a C-string to lowercase, the loop might look like this:
    Code:
    for (int i = 0; str[i] != '\0'; ++i) {
        str[i] = tolower(str[i]);
    }
    Of course, you might use pointer arithmetics instead of indexed access. However there is no need to call strlen for that at all. Both strlen and this loop work the same way: keep looping until the current character is '\0'.

    -----------
    And one more note on coding style. It might be wiser in C++ to declare variables at the smallest scope you are using them. In case of for-loops the loop counter can be declared as part of the for statement (see example above).

    As to your mistake of using the same variable i for two nested loops - had you declared the two i's in the for statements a) the compiler could have warned you about problems with variable names, and b) if you ignored the warnings the code would have still done the right thing. (A variable declared in a nested scope shadows the variable of the same name in the outer scope.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM