C++ fwrite函数';vs2015和vs2012的差异
调用C++ fwrite函数';vs2015和vs2012的差异,c++,C++,调用fclose()后调用fwrite()时,vs2012没有抛出错误,但在VS 2015中它抛出错误 代码如下: int main() { FILE* m_pFile; char* filePath = "D:\\test_vs2015.txt"; fopen_s(&m_pFile, filePath, "w+"); if (m_pFile == NULL) return 0; char* str = "strNum text"
fclose()
后调用fwrite()
时,vs2012没有抛出错误,但在VS 2015中它抛出错误
代码如下:
int main()
{
FILE* m_pFile;
char* filePath = "D:\\test_vs2015.txt";
fopen_s(&m_pFile, filePath, "w+");
if (m_pFile == NULL)
return 0;
char* str = "strNum text";
int flag = 0;
while (str[flag++] != '\0');
int num = fwrite(str, 1, flag - 1, m_pFile);
int fcloseFlag = fclose(m_pFile);
if (m_pFile == NULL)
printf("m_pFile == NULL ");
else
printf("m_pFile != NULL ");
int writeNum = fwrite(str, 1, flag - 1, m_pFile);
return 0;
}
文件关闭函数不以整数形式返回,请使用bool 我确信在调用
fclose
之后调用fwrite
是未定义的行为。当我在调用fclose()之后调用fwrite()时,我看不到问题,只是观察。如果问题是“为什么”,那么答案是行为没有定义。你这样做是无效的,不知道会发生什么。这里有问题吗?