我们有一个使用Turbo C ++的学校项目。所以请不要讨厌
简单的疑问:如何检查数据文件(扩展名为.dat)中是否存储了任何数据,有没有可以使用的功能?如何使用?
以及如何清除其中的所有数据的数据文件。
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <unistd.h>
size_t getFileSize(const std::string &filename) {
struct stat s;
if(stat(filename.c_str(), &s) != 0) {
return 0;
}
return s.st_size;
}
void clearFile(const std::string &filename)
{
truncate(filename.c_str(), 0);
}
int main(int argc, char **argv)
{
std::cout << "File size: " << getFileSize("../test.dat") << std::endl;
clearFile("../test.dat");
std::cout << "File size: " << getFileSize("../test.dat") << std::endl;
return 0;
}