0

次のコードでエラーが発生するのはなぜですか? コードと写真を見てください。修正方法 ここに画像の説明を入力

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;
}

しかし、この作品は正しい

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;
}

ここに画像の説明を入力 完全なコード

#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;
}
4

2 に答える 2

1

ファイルは何らかの理由で開くことができず、fileNULL です。ファイルが開いていることを常に確認してください。

また、あなたが何をしていると思うのだろうかfs

于 2012-10-28T16:59:01.533 に答える
0

indexing.xml現在のディレクトリでファイルを探しています。

VC プロジェクトのデフォルトは、exe ファイルのディレクトリに設定された現在のディレクトリです2012\Projects\FindPattern\Debug。ファイルはそこにありません。1 つ上のフォルダーにあります。

于 2012-10-29T14:26:40.247 に答える