是否具有检查数据文件中数据的功能

问题描述 投票:0回答:3

我们有一个使用Turbo C ++的学校项目。所以请不要讨厌

简单的疑问:如何检查数据文件(扩展名为.dat)中是否存储了任何数据,有没有可以使用的功能?如何使用?

以及如何清除其中的所有数据的数据文件。

c++ turbo-c++
3个回答
0
投票
#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; }

0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.