none
getline (fstream) RRS feed

  • Question

  • it seems visual c++ has an identity crisis with this function, even though is one of the most basic things related to file reading, and file reading is one of the most basic and common things in any program

    if you check this page, a basic tutorial on standard c++ that every compiler should run with no problem

    http://www.cplusplus.com/doc/tutorial/files/

    there is this piece of code

    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while ( getline (myfile,line) )
        {
          cout << line << '\n';
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }

    quite basic and if you copy paste it in visual c++, it will work as expected. I checked because I wanted to know why some piece of code that works elsewhere gives errors when trying to compile using visual c++

    yet there is this sentence:

    <var>"while</var> ( getline (myfile,line) )"

    you would think that you could do something like this:

    bool _could_read = getline (myfile,line);

    but no, you can't. The result depends on the mood of visual c at that moment. Sometimes I get an error message about that there is no suitable conversion from blah blah blah, the thing is you can't, because even though the ifs and whiles take a bool or an int that is converted to a bool, somehow you can not save the getline result in a bool even if used inside ifs and whiles as a bool. By the way, you can also use it inside it an if, if visual c is in the mood (not always)

    But more interesting: 'getline': identifier not found. uh? the .cpp includes a .h where there is the line #include <fstream>, but somehow that does not seem to be enough. So in the .cpp itself, I have these two lines

    #include <iostream>
    #include <fstream>

    and having those two lines in the same file, it tells me 'getline': identifier not found

    By the way, this is code that compiles with no problem in mingw compilers; I am using visual c++ just to check the syntax and checking that I am using standard expressions, but it seems it won't be easy because of a poor quality of the visual c++ compiler and they making their own syntax rules

            _file >> _read_int;
            //bool _could_read = getline(_file, _read_string); // can not be converted to bool
            //if (_could_read == false)
            if (/*_file.fail() == true ||*/ !getline(_file, _read_string) )
                throw ( exception() );
            _unsigned_char = (ubyte)_read_int; // ubyte defined as unsigned char elsewhere
            // --

    Please fix that

    Tuesday, September 8, 2020 8:03 PM

All replies

  • It's std::getline, not fstream::getline (in fact getline is added to the std namespace by iostream, not fstream) ... so while you included iostream and fstream you did not evidently:

    using namespace std;

    The identity crisis appears to be in your documentation source.  Try cplusplus.com

    Edit:  I noticed your remarks about how your faulty/non-standard code works fine in MinGW and therefore MS and VS are in the wrong here.  GNU C is not a legitimate C standard and implicit includes and usings are GNU C.  I've made similar mistakes and blamed MS/VS, but eventually I figured out how to be a good programmer instead of a GNU fan.


    Before you can learn anything new you have to learn that there's stuff you don't know.



    Tuesday, September 8, 2020 8:18 PM
  • thanks, but no use

    In the cpp I included the line

    using namespace std;

    first below then above the line

    #include <iostream>

    which means both these lines are in the same file that generates the errors. I still get the 'getline': identifier not found errors, except when I placed the std:: before the getline

            _file >> _read_int;
            if (_file.fail() == true || !std::getline(_file, _read_string) ) // read till end of line
                throw ( exception() );
            _unsigned_char = (ubyte)_read_int;

    which changes the error from the previous one to 'getline': is not a member of 'std' (despite having both #include <iostream> and using namespace std; in the same file)

    ---

    edit: actually now there are two errors in the same line; both the 'not a part of std' and 'identifier not found' generate on the same lines

    Tuesday, September 8, 2020 8:35 PM
  • std::getline doesn't return a bool.  It returns an object that you can cast to a bool.

    So this works (tested in VS2015 just to demonstrate obtaining a bool value)

    bool bgetline = true;
    while (bgetline)
    {
        bgetline = static_cast<bool>(getline(myfile, line));
        cout << line << '\n';
    }


    • Edited by RLWA32 Tuesday, September 8, 2020 8:52 PM
    Tuesday, September 8, 2020 8:49 PM
  • I don't know if this makes any sense, but it only compiled with no errors till I added this line

    #include <string> // getline in Visual Studio

    Tuesday, September 8, 2020 8:51 PM
  • I don't know if this makes any sense, but it only compiled with no errors till I added this line

    #include <string> // getline in Visual Studio

    I suppose that would be because multiple signatures of getline require one parameter of type std::string... it would explain why the string header is included in the original example you presented too.

    Before you can learn anything new you have to learn that there's stuff you don't know.


    Tuesday, September 8, 2020 8:57 PM