关闭文件fc时出现问题。 这是我的代码
#include <stdio.h>
#include <wchar.h>
int main()
{
FILE* fc = fopen("template.txt","rt,ccs=UTF-8");
wchar_t subStr[2300];
fread(subStr,sizeof(wchar_t),2300,fc);
wchar_t* scrStr=new wchar_t[2300];
wcscpy(scrStr,subStr);
fclose(fc);
return 0;
}
答案 0 :(得分:0)
首先可能无法正确打开文件。您必须检查fopen
返回的流的“句柄”是否为NULL
。下面的代码检查文件'handle',更改了几个位以使其'合理'C。
#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t* scrStr = NULL;
FILE* fc = fopen("template.txt","rt,ccs=UNICODE");
// Check that the file was opened.
if(NULL != fc)
{
wchar_t subStr[2301];
size_t wcharsRead = fread(subStr, sizeof wchar_t , 2300, fc);
fclose(fc);
// Check if anything was read.
if(0 != wcharsRead)
{
// Terminate the string in the temporary buffer.
subStr[wcharsRead] = `\0`;
// Allocate a smaller string (use new, if you're in C++).
if(scrStr = malloc(1 + sizeof wchar_t * wcharsRead), NULL != scrStr)
{
// Copy the useful bit into the new string.
wcscpy(scrStr, subStr);
}
}
}
else
{
// Do some error handling
printf("Unable to open file. Error code: %d\r\n",errno);
}
// Check if a string was read.
if(NULL != scrStr)
{
// Do something with scrStr
// Free the string once it's finished with (use delete if you're in C++).
free(scrStr);
}
return 0;
}