How do I use UTF-8 encoded ftell and fseek with Visual Studio?

I want to go back to a known position in a file. So I use ftell to store the position, and later I use fseek to return.

I am aware of the limitations to find a specific position. But that doesn't apply to my case. I have a well-known position and I want to return to it.

Here is some test code I wrote to simplify the problem. It just stores the position in the file. Reads a line, returns, and rereads a line. And I get the assertion because sometimes I get two different results.

#include "stdlib.h"
#include "crtdbg.h"

int main()
{
    FILE *fStream = nullptr;
    if (_tfopen_s(&fStream,_T("TestFTell.txt"), _T("rt,ccs=UTF-8")))
        return 0;

    TCHAR szBuffer1[256], szBuffer2[256];    
    for (;;)
    {
        auto pos = ftell(fStream);
        _ASSERT(pos!=-1);
        if (!_fgetts(szBuffer1, _countof(szBuffer1), fStream))
            break;
        auto retval1 = fseek(fStream,pos,SEEK_SET);
        _ASSERT(retval1!=-1);
        auto retval2 = _fgetts(szBuffer2, _countof(szBuffer2), fStream);
        _ASSERT(retval2!=0);
        _ASSERT(_tcscmp(szBuffer1, szBuffer2)==0);      
    }
    return 0;
}

      

The program is compiled for use in Unicode. The text file just contains some strings and some strings have UTF-8 characters. Here's a test file.

I tested it on VS-2010, VS-2013, VS-2015. In all versions, I get ASSERT.

Is this a bug or am I misunderstanding ftell / fseek?

How can I go back to the previous read position in the file?

Or did I find a bug in the CRT?

PS: In VS-2010, I already filed a bug for this case . They wrote that this will be fixed. The stitches that it was never fixed.

+3


source to share





All Articles