我有一个源文件 file1
和目标文件 file2
, 这里我必须从 file1
移动内容至 file2
.
所以我必须先做一些验证。
我可以使用这个检查:
fp = fopen( argv[1],"r" );
if ( fp == NULL )
{
printf( "Could not open source file\n" );
exit(1);
}
这是我到目前为止所尝试的。
最佳答案
C版:
if (NULL != fp) {
fseek (fp, 0, SEEK_END);
size = ftell(fp);
if (0 == size) {
printf("file is empty\n");
}
}
C++ 版本(从 here 窃取):
bool is_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
关于c - 如何使用C检查文件是否有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13566082/