Like Tree2Likes
  • 1 Post By tabstop
  • 1 Post By vart

gcount() always returning 0

This is a discussion on gcount() always returning 0 within the C++ Programming forums, part of the General Programming Boards category; Whether it's a text file or a binary file gcount() always returning 0 but int size gets the values read. ...

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

    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;
         }
    Compiler MSVC++ 2013 with Code::Blocks.

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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    Ducky likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thank you, that was useful.
    Last edited by Ducky; 10-20-2013 at 03:43 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

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, 07: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, 10:06 PM
  5. gcount with files
    By DMaxJ in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2002, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21