I haven't got any luck finding why this program wouldn't work.

Here's the code.

int main (void)
{
    ifstream file;
    char *streamer = NULL;
    
    file.open("now.txt", ios::in | ios::binary);
    
    if (file.is_open())
    {
        while (!file.eof())
        {
            streamer = new char[33];
            cout << "Prestream: " << streamer << endl;
            file.read(streamer, 32);
            cout << "gcount: " << file.gcount() << endl;
            cout << strlen(streamer) << endl;
            cout << streamer << endl << endl;
            delete [] streamer;
        }
    }
}

The problem is when the file get pointer is almost at the end,
the program's supposed to take only the remaining characters,
so if there are only 12 characters left, streamer will only have 12 characters, the problem is that streamer will still have 32, the mysterious 20 characters coming from the previous read() operation.

Any help would be appreciated.

I haven't got any luck finding why this program wouldn't work.

Here's the code.

int main (void)
{
    ifstream file;
    char *streamer = NULL;
    
    file.open("now.txt", ios::in | ios::binary);
    
    if (file.is_open())
    {
        while (!file.eof())
        {
            streamer = new char[33];
            cout << "Prestream: " << streamer << endl;
            file.read(streamer, 32);
            cout << "gcount: " << file.gcount() << endl;
            cout << strlen(streamer) << endl;
            cout << streamer << endl << endl;
            delete [] streamer;
        }
    }
}

The problem is when the file get pointer is almost at the end,
the program's supposed to take only the remaining characters,
so if there are only 12 characters left, streamer will only have 12 characters, the problem is that streamer will still have 32, the mysterious 20 characters coming from the previous read() operation.

Any help would be appreciated.

It sounds you forgot to initialize the newly created buffer. streamer = new char[33]; This statemnet creates a new buffer with 33 bytes, but the buffer contains unexpected data rather then zero-initialized data. So, you shout initialize it with zero manually.

streamer = new char[33];
std::fill_n(streamer, 33, '\0');

Or the better way to do that is using a std::vector to automatically manage and initialize the buffer:

...
            std::vector<char> streamer(33);
            cout << "Prestream: " << &streamer.front() << endl;
            file.read(&streamer.front(), 32);
            cout << "gcount: " << file.gcount() << endl;
            cout << strlen(&streamer.front()) << endl;
            cout << &streamer.front() << endl << endl;
            //delete [] streamer;
            ..

I'll bet its your while (!file.eof()) statement. See this ( .eof() is the same as feof()

Hey thanks... The solution was indeed that the streamer always terminated at byte 32, so even if streamer only received 12 bytes, it would still print 32 bytes, the 20 extra characters coming from the previous allocation...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.