Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help: I cannot use getline(...)

Help: I cannot use getline(...)

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpios
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GavinWang
    wrote on last edited by
    #1

    fstream file; char buffer[MAXBUFSIZE]; string line; file.open(..., ios_base::in); file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); //both of the above fail to work //and the vc.net2003 print such error info: //error C2663: “std::basic_istream<_Elem,_Traits>::getline” : //2 //overloaded functions lack a legal conversion of pointer "this".

    M 1 Reply Last reply
    0
    • G GavinWang

      fstream file; char buffer[MAXBUFSIZE]; string line; file.open(..., ios_base::in); file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); //both of the above fail to work //and the vc.net2003 print such error info: //error C2663: “std::basic_istream<_Elem,_Traits>::getline” : //2 //overloaded functions lack a legal conversion of pointer "this".

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      This works fine: -------------------------------------- #include #include using namespace std; void main() { fstream file; const int MAXBUFSIZE = 256; char buffer[MAXBUFSIZE]; file.open("a.txt", ios_base::in); do { file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); string s = buffer; cout << s.c_str() << endl; } while (! file.eof() ); } Maxwell Chen

      H 1 Reply Last reply
      0
      • M Maxwell Chen

        This works fine: -------------------------------------- #include #include using namespace std; void main() { fstream file; const int MAXBUFSIZE = 256; char buffer[MAXBUFSIZE]; file.open("a.txt", ios_base::in); do { file.getline(buffer, MAXBUFSIZE-1); //getline(file, line); string s = buffer; cout << s.c_str() << endl; } while (! file.eof() ); } Maxwell Chen

        H Offline
        H Offline
        Henrik Stuart
        wrote on last edited by
        #3

        Might I suggest something more like this:

        #include <iostream>
        #include <fstream>
        #include <string>

        int main() {
        std::ifstream file("filename");
        std::string line;

        while (std::getline(file, line)) {
        std::cout << line << '\n';
        }

        return 0;
        }

        Just a couple of points:

        • C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
        • Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
        • Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.

        Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

        A M G 3 Replies Last reply
        0
        • H Henrik Stuart

          Might I suggest something more like this:

          #include <iostream>
          #include <fstream>
          #include <string>

          int main() {
          std::ifstream file("filename");
          std::string line;

          while (std::getline(file, line)) {
          std::cout << line << '\n';
          }

          return 0;
          }

          Just a couple of points:

          • C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
          • Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
          • Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.

          Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          thank you for the response. Finnally, I figured out the key problem. the fstream object is the data member of my class not declared mutable, but in the member function declared const, the fstream.getline() failed. //ps: the error information vc.net2003 offers me //just confuses me:(

          1 Reply Last reply
          0
          • H Henrik Stuart

            Might I suggest something more like this:

            #include <iostream>
            #include <fstream>
            #include <string>

            int main() {
            std::ifstream file("filename");
            std::string line;

            while (std::getline(file, line)) {
            std::cout << line << '\n';
            }

            return 0;
            }

            Just a couple of points:

            • C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
            • Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
            • Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.

            Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            Henrik, you got my 5! ;) Basically I agree with all of your view points, and they are quite true. It reminds me to pay efforts in studying the Standard Library. Regarding to the prototype of main, you are right too, as we don't find any 'void main()' in Stroustrup's book. It's just my bad custom for simplicity, since in the MSDN Library it is stated: Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function. Anyway, I like your attitude! :) Maxwell Chen

            1 Reply Last reply
            0
            • H Henrik Stuart

              Might I suggest something more like this:

              #include <iostream>
              #include <fstream>
              #include <string>

              int main() {
              std::ifstream file("filename");
              std::string line;

              while (std::getline(file, line)) {
              std::cout << line << '\n';
              }

              return 0;
              }

              Just a couple of points:

              • C++ defines main as: "int main()" or "int main(int argc, char* argv[])". There's nothing in the code that limits us to Visual C++ and its non-standard "void main()" extension.
              • Using std::getline (defined in "string") we avoid using a char* buffer as we have to with std::basic_istream<...>::getline.
              • Invoking s.c_str() on something we write out using iostreams anyway might incur the following overhead: a char* buffer that can contain the contents is allocated, the contents of s is copied over into it, a terminating null character is added and this buffer is returned. This is because the contents of std::string isn't guaranteed to be null terminated.

              Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

              G Offline
              G Offline
              GavinWang
              wrote on last edited by
              #6

              thank you for the response. Finnally, I figured out the key problem. the fstream object is the data member of my class not declared mutable, but in the member function declared const, the fstream.getline() failed. //ps: the error information vc.net2003 offers me //just confuses me:(

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups