0

I am writing an iOS app which uses a piece of c++ code which is reading pieces of data from a file using istream. The code works but its used repeatedly and it eventually hits a limit of reads and then something goes wrong. It appears to read in successfully but gcount returns 0. This is not specific to a certain file or case because after the app is restarted everything will pick up from where it left off and fail once the limit is hit again. It feels like there is some istream session that gets into a bad state that I can't get out of. I am closing the files I open.

This is how I open the streams

iFiles.push_back(new ifstream([[[arrayOfPaths objectAtIndex:i] absoluteString] cStringUsingEncoding:NSASCIIStringEncoding] , ios::in | ios::binary));

Then I have a loop where I read in pieces of this file:

while (!iStreams[0]->eof())
{
    vector<vector<int64_t> > matrix;
    int length = 0;
    for (int i = 0; i < rows; i++)
    {
        char * buffer = new char [DEMUX_BUFFER];

        iStreams[i]->read(buffer, DEMUX_BUFFER);

        int int64Groups;

        if(iStreams[i]->gcount() == 0)
        {
            //this is the catch for the bad case but buffer contains something even though count is 0
        }
        .....

This is a hard question to ask so please let me know if you need more information. I am hopping someone out there has had a similar issue.

Thank you

rooster117
  • 5,502
  • 1
  • 21
  • 19
  • 4
    well for one you shouldnt use `eof()`: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – dwcanillas May 13 '15 at 18:00

1 Answers1

3

I think I need more code to be able to give a better answer, but from your code, I can tell you where I'd first look:

istream::gcount returns the number of characters extracted by the last unformatted input operation performed on the object.

This means that when the end of the file is reached, gcount will return 0, because the last reading operation was't able to read any new data.

Now, it is normal to have some random bits in the buffer, since the space was allocated, but you never zeroed the buffer. Therefore, the buffer will contain some random bits from the memory where it was randomly allocated. In order to avoid confusion, you can add a statement that will clear any garbage that the buffer initially has, by adding this statement, right after its allocation:

memset(buffer, 0, sizeof(buffer));

In other words, when your gcount returns 0, it means that there was no new bit of data read from iStreams[i].

I hope this helps!

-- Alex

  • Thank you, I realize now that i wasn't zeroing the buffer. My issue still remains though and I realize my question didn't explain it enough. I'll try to come up with a minimized version of the code to make it readable – rooster117 May 13 '15 at 20:12
  • 1
    My question was not the problem. I think I finally got to the bottom of it and I was passing in a vector of ifstreams to a method expecting istreams and I believe it was not closing correctly when I was trying to close the files eventually causing some limit to be reached. It was a weird error. – rooster117 May 13 '15 at 22:07
  • Thank you for all of your help. It gave me some clues to find the root of the issue. – rooster117 May 13 '15 at 22:10