为什么下一个代码会出错?看代码和图片。如何修复它
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;
}