[英]Reading unicode file
Why next code give an error? 为什么下一个代码给出错误? Look code and pictures.
查看代码和图片。 How to fix it
如何修复
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;
}
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;
}
Your file has failed to open for some reason and file
is NULL. 您的文件由于某种原因无法打开,并且
file
为NULL。 Always check that files open. 始终检查文件是否打开。
Also got to wonder what you think you are doing with fs
. 也想知道您对
fs
看法。
You're looking for file indexing.xml
in the current directory. 您正在当前目录中寻找文件
indexing.xml
。
The default for a VC project is the current directory set to the directory of the exe file, 2012\\Projects\\FindPattern\\Debug
. VC项目的默认值为当前目录,该目录设置为exe文件的目录
2012\\Projects\\FindPattern\\Debug
。 The file is not there, it is one folder up. 该文件不存在,它是一个文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.