Thread: gcount() always returning 0

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    gcount() always returning 0

    Whether it's a text file or a binary file gcount() always returning 0 but int size gets the values read.
    Why?

    Code:
       
        int size;
        string str;
        ifstream ifs;
        ifs.open(file, ifstream::binary);
     
        while(!ifs.eof())
        {
            getline(ifs, str);
            cout << ifs.gcount() << " characters read: "  << '\n';
            size = str.size();
            cout << "size " << size << endl;
         }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think that version of getline changes gcount (just the one that reads into a char* instead).

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    It explains for C - but same idea applicable to C++ - so you could benefit from reading this FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you, that was useful.
    Last edited by Ducky; 10-20-2013 at 03:43 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. gcount() question ?
    By Tropod in forum C++ Programming
    Replies: 0
    Last Post: 11-13-2009, 08:06 PM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Help w/ gcount()
    By Ignited in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2006, 11:06 PM
  5. gcount with files
    By DMaxJ in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2002, 09:43 AM