5

I am reading some data using an istream and read(). I would like to know if I can just test gcount() for the bytes or if I need to test some combination of good(), eof(), etc before calling gcount(). In other words, is gcount() always set after a read() even if that read failed due to EOF or some other internal problem?

Also if this is described in the standard or somewhere that you can cite. I'm using cplusplus.com as a reference and it says that gcount "Returns the number of characters extracted by the last unformatted input operation performed on the object." Can I interpret statements like "last operation" to mean last operation, whatever the outcome?

Evg
  • 25,259
  • 5
  • 41
  • 83
loop
  • 3,460
  • 5
  • 34
  • 57
  • I expect you can call gcout() at any state, as long as the stream is constructed. What are you ACTUALLY trying to achieve? If you call `read` and the next thing that happens is that the file reaches EOF, is it acceptable that gcount() returns 0? – Mats Petersson Feb 27 '14 at 23:58
  • 1
    `gcount()` counts all extracted characters. And it's possible for the stream to extract characters and then later fail during the same I/O operation. It's not reset to `0` if an extraction fails. – David G Feb 28 '14 at 00:00

1 Answers1

6

Is gcount() always set after a read() even if that read failed due to EOF or some other internal problem?

Yes

gcount()'s job is solely to the return the number of characters extracted from the last unformatted input operation. The Standard makes no distinction between the value of gcount() when an extraction succeeds and when it fails. And obviously if the input operation could not extract characters then the value will be 0.

So all you need to test if an extraction succeeded is by using it as the condition. Use only gcount() in the condition only if you wish to determine if a certain amount of characters were extracted.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
David G
  • 94,763
  • 41
  • 167
  • 253
  • In your example wouldn't the if statement evaluate false if an attempt to read 5 bytes returns some lesser n like 4? Right now I have code that loops to read a stream like `is.read(data,sizeof data); if(is.gcount()) {//process bytes read} else {//out of bytes to read}` – loop Feb 28 '14 at 02:47
  • No, if `is.gcount()` is non-zero, then it evaluates to `true`. – David G Feb 28 '14 at 21:08
  • 1
    Sorry if this is a bother I'm just trying to wrap my head around your example which is why I haven't accepted the answer yet. is.read() returns is, which after a read for more bytes than available is false. For example, this returns false if one.txt is size <1024: `ifstream file("one.txt"); istream &is = file; char data[1024]; if (is.read(data, sizeof data) && is.gcount()) { cout << "true" << endl; } else { cout << "false" << endl; }` – loop Mar 01 '14 at 03:30
  • @test Yes, but that has nothing to do with `gcount()`. That has to do with the fact that the stream hit the EOF while extracting. So it set the `failbit` in the stream which is the reason why the first condition returns false. – David G Mar 01 '14 at 03:40
  • @test I actually think I made a mistake while answering in my post. You **shouldn't** use `&& is.gcount()` as a second condition. Just use it to find out how many characters were extracted, simple as that. – David G Mar 01 '14 at 03:45
  • Ok thanks. Would you mind editing your answer to clarify or remove the example, then I will accept the answer. – loop Mar 01 '14 at 05:09