abilash
abilash

Reputation: 897

Reading unicode file

Why next code give an error? Look code and pictures. How to fix it enter image description here

wchar_t *GetLine(wchar_t *fileName=L"indexing.xml", wchar_t endSymbol = '\n')
{
    FILE *file = NULL;
    int sz;
    _wfopen_s(&file, fileName, L"r");
    std::wifstream fs (file);
    int size;
    wchar_t wchr[1];
    size = 0;
    do
    {
        sz = fread(&wchr,sizeof(wchar_t),1,file);
        if(!sz)
        {
            break;
        }
        tempGetLine[size] = wchr[0];
        size++;
    }while(wchr[0] != endSymbol);
    tempGetLine[size] = '\0';
    position += (size);
    fs.close();
    return tempGetLine;
}

but this work correct

wchar_t *GetLine(wchar_t *fileName=L"indexing.xml", wchar_t endSymbol = '\n')
{
    hReadFile = CreateFileW(L"indexing.xml",GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    SetFilePointer(hReadFile,sizeof(wchar_t) * position, NULL, FILE_BEGIN);
    int size;
    wchar_t wchr[1];
    DWORD dw;
    size = 0;
    do
    {
        ReadFile(hReadFile, wchr, sizeof(wchar_t), &dw, NULL);
        if(!dw)
        {
            break;
        }
        tempGetLine[size] = wchr[0];
        size++;
    }while(wchr[0] != endSymbol);
    tempGetLine[size] = '\0';
    position += (size);
    return tempGetLine;
}

enter image description here Full code

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <Windows.h>
int position = 0;
wchar_t tempGetLine[500];
wchar_t *GetLine(wchar_t *fileName=L"indexing.xml", wchar_t endSymbol = '\n')
{
    FILE *file = NULL;
    int sz;
    _wfopen_s(&file, L"C:\\indexing.xml", L"r");
    std::wifstream fs (file);
    int x = GetLastError();
    fseek(file,sizeof(wchar_t) * position,SEEK_SET);
    int size;
    wchar_t wchr[1];
    size = 0;
    do
    {
        sz = fread(&wchr,sizeof(wchar_t),1,file);
        if(!sz)
        {
            break;
        }
        if(wchr[0] >= L'А')continue;            //Only for console application
        tempGetLine[size] = wchr[0];
        size++;
    }while(wchr[0] != endSymbol);
    tempGetLine[size] = '\0';
    position += (size);
    fs.close();
    return tempGetLine;
}

Upvotes: 0

Views: 3806

Answers (2)

Andriy
Andriy

Reputation: 8604

You're looking for file indexing.xml in the current directory.

The default for a VC project is the current directory set to the directory of the exe file, 2012\Projects\FindPattern\Debug. The file is not there, it is one folder up.

Upvotes: 0

john
john

Reputation: 87959

Your file has failed to open for some reason and file is NULL. Always check that files open.

Also got to wonder what you think you are doing with fs.

Upvotes: 1

Related Questions