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
c ++ c visual-studio utf-8 crt


source to share


No one has answered this question yet

Check out similar questions:

1419
Using Git with Visual Studio
1104
.gitignore for Visual Studio projects and solutions
989
Can you get Visual Studio to run as administrator on Windows 8?
815
Difference between Build Solution, Rebuild Solution and Pure Solution in Visual Studio?
801
Should I add the Visual Studio .suo and .user files to the original control?
758
What are the different Create Action settings in Visual Studio project properties and what do they do?
745
How do I "add an existing item" my entire directory structure in Visual Studio?
741
How do you format your code in Visual Studio?
726
How can I add an existing directory tree to a project in Visual Studio?
613
How do you count lines of code in a Visual Studio solution?



All Articles
Loading...
X
Show
Funny
Dev
Pics