dhein
dhein

Reputation: 6555

Why fread() returns 0?

I don't udnerstand that. The File exists. It has contant which length is fitting the value hold by sizeIndexI. I'm at the begining of the File(Am I not?) and anyway It wont read from that file...

Ofc. the file also was succesfully opened before. (In this case with a+) And the access permissions for the File are ofc. also given.

fpNewsPageLogger = fopen ("/NewsLogx", "a+");

if (fpNewsPageLogger == nullptr)
{
            /*...*/
}
else
{
        fseek (fpNewsPageLogger, 0 ,SEEK_END);
        sizeIndexI = ftell (fpNewsPageLogger);
        rewind (fpNewsPageLogger);
        DebugLogMsg10 (pDebugLogger, sizeThreadID, "ReadAmount:%d IndexI:%d!", sizeBytesRead, sizeIndexI);

        cpTmpNews = calloc (sizeIndexI, sizeof(char));

        if (cpTmpNews == nullptr)
        {
            fclose (fpNewsPageLogger);
            return;
        }

        sizeBytesRead = fread (cpTmpNews, sizeof (char), sizeIndexI, fpNewsPageLogger);

    /*...*/
}

Is there anything I'm not thinking about?

Upvotes: 0

Views: 2886

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320729

Firstly, standard library is not required to meaningfully support seeking from SEEK_END. Did you check the value of sizeIndexI? Maybe it is simply zero? If you ask fread to read zero elements, it expectedly returns zero.

Secondly, you are opening your stream as a text stream. For a text stream values returned by ftell do not generally have any meaningful numerical semantics. In general case ftell for text streams returns an implementation defined encoding of the current position, not the byte offset from the beginning of the file. If you want to work with your stream as binary stream, add "b" to fopen

fpNewsPageLogger = fopen ("/NewsLogx", "ab+");

Upvotes: 2

Related Questions