爲什麼下一個代碼會給出錯誤?看代碼和圖片。如何修復 讀取unicode文件
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;
}
哪條線是102線? – hyde
@hyde line 102在Microsoft C運行時。 – john
@hyde會在fread.c庫中的第102行;這個問題不重要。 –