- 注册
- 2024/02/25
- 消息
- 93
- 金粒
- 816金粒
闲着无聊用 C++ 写了个填充硬盘的东西。
C++ 标准为 C++ 20,需要头文件
C 语言应该也可以实现,不过可能比较繁琐。
代码解释(CodeGeeX 生成的,懒得自己写了):
使用示例:
总结:什么用都没有(
C++:
class CDiskFiller{
#define FILE_NAME "FILL."
#define FILE_EXTENSION_NAME ".txt"
private:
std::atomic<short>completeThreadCounter;
protected:
short numThread;
long numSingleThreadCycle;
std::string filledString;
auto fill(short fsId){
std::ofstream fs(\
FILE_NAME+std::to_string(fsId)+FILE_EXTENSION_NAME,\
std::ios::out|std::ios::trunc\
);
for(long i{};i<numSingleThreadCycle;++i){
fs<<filledString;
}
++completeThreadCounter;
return;
}
public:
CDiskFiller(){}
virtual ~CDiskFiller(){}
auto set(short th,long cycle,std::string str){
if((th<=0)||(cycle<=0)||(str.length()==0)){
std::clog<<"[ERROR] set"<<std::endl;
return false;
}
this->numThread=th;
this->numSingleThreadCycle=cycle;
this->filledString=str;
std::clog<<"[ O K ] set"<<std::endl;
return true;
}
auto run(){
if((numThread<=0)||(numSingleThreadCycle<=0)||(filledString.length()==0)){
std::clog<<"[ERROR] run"<<std::endl;
return false;
}
for(short i{};i<numThread;++i){
std::thread(&CDiskFiller::fill,this,i).detach();
}
while(completeThreadCounter<numThread){
sleep(1);
}
std::clog<<"[ O K ] run"<<std::endl;
return true;
}
auto clean(){
if(numThread<=0){
std::clog<<"[ERROR] clean"<<std::endl;
return false;
}
for(short i{};i<numThread;++i){
remove((FILE_NAME+std::to_string(i)+FILE_EXTENSION_NAME).c_str());
}
std::clog<<"[ O K ] clean"<<std::endl;
return true;
}
};
C++ 标准为 C++ 20,需要头文件
iostream
,fstream
,thread
,atomic
,unistd.h
。工具链为 msys2 的 mingw-w64-ucrt-x86_64-toolchain
,g++ 版本为 14.2.0。编译命令为:
代码:
g++ main.cpp -C -fexec-charset=GBK -std=gnu++23 -Wall -Wextra -pipe -m64 -DNDEBUG -static -Ofast -Ofast -Os -flto -o main.exe
C 语言应该也可以实现,不过可能比较繁琐。
代码解释(CodeGeeX 生成的,懒得自己写了):
这是一个C++类,名为CDiskFiller
,用于填充磁盘空间。以下是代码的主要功能和实现原理:
1. 定义常量:定义了两个常量,FILE_NAME
和FILE_EXTENSION_NAME
,分别用于生成文件名和扩展名。
2. 定义成员变量:使用std::atomic
定义了一个short
类型的成员原子变量completeThreadCounter
,用于记录已完成填充任务的线程数量。
3. 定义保护成员变量:定义了三个保护成员变量,numThread
、numSingleThreadCycle
和filledString
,分别用于存储线程数量、每个线程的填充循环次数和填充字符串。
4. 定义自动填充函数fill
:定义了一个自动填充函数fill
,该函数接受一个参数fsId
,用于生成文件名。函数内部创建了一个std::ofstream
对象fs
,用于写入文件。然后,使用std::string
类型的成员变量filledString
填充文件,直到numSingleThreadCycle
次。最后,将completeThreadCounter
加1,表示完成一个填充任务。
5. 定义构造函数:定义了一个构造函数,用于初始化CDiskFiller
对象。
6. 定义析构函数:定义了一个析构函数,用于清理CDiskFiller
对象。
7. 定义设置函数set
:定义了一个设置函数set
,用于设置线程数量、填充循环次数和填充字符串。函数内部首先检查输入参数是否有效,如果无效,则输出错误信息并返回false
。如果有效,则将输入参数赋值给成员变量,并输出成功信息。
8. 定义运行函数run
:定义了一个运行函数run
,用于启动填充任务。函数内部首先检查输入参数是否有效,如果无效,则输出错误信息并返回false
。如果有效,则创建numThread
个线程,每个线程执行fill
函数,并使用std::thread
的detach()
方法使线程分离。然后,使用while
循环等待所有线程完成填充任务。最后,输出成功信息并返回true
。
9. 定义清理函数clean
:定义了一个清理函数clean
,用于清理生成的文件。函数内部首先检查输入参数是否有效,如果无效,则输出错误信息并返回false
。如果有效,则删除numThread
个文件,并输出成功信息。
注意:这个类主要用于填充磁盘空间,但可能存在一些潜在问题,例如文件名冲突、磁盘空间不足等。在实际使用中,需要谨慎使用,并确保正确设置参数。
使用示例:
C++:
#include<iostream>
#include<fstream>
#include<thread>
#include<atomic>
#include<unistd.h>
class CDiskFiller{
#define FILE_NAME "FILL."
#define FILE_EXTENSION_NAME ".txt"
private:
std::atomic<short>completeThreadCounter;
protected:
short numThread;
long numSingleThreadCycle;
std::string filledString;
auto fill(short fsId){
std::ofstream fs(\
FILE_NAME+std::to_string(fsId)+FILE_EXTENSION_NAME,\
std::ios::out|std::ios::trunc\
);
for(long i{};i<numSingleThreadCycle;++i){
fs<<filledString;
}
++completeThreadCounter;
return;
}
public:
CDiskFiller():\
completeThreadCounter{}{}
virtual ~CDiskFiller(){}
auto set(short th,long cycle,std::string str){
if((th<=0)||(cycle<=0)||(str.length()==0)){
std::clog<<"[ERROR] set"<<std::endl;
return false;
}
this->numThread=th;
this->numSingleThreadCycle=cycle;
this->filledString=str;
std::clog<<"[ O K ] set"<<std::endl;
return true;
}
auto run(){
if((numThread<=0)||(numSingleThreadCycle<=0)||(filledString.length()==0)){
std::clog<<"[ERROR] run"<<std::endl;
return false;
}
for(short i{};i<numThread;++i){
std::thread(&CDiskFiller::fill,this,i).detach();
}
while(completeThreadCounter<numThread){
sleep(1);
}
std::clog<<"[ O K ] run"<<std::endl;
return true;
}
auto clean(){
if(numThread<=0){
std::clog<<"[ERROR] clean"<<std::endl;
return false;
}
for(short i{};i<numThread;++i){
remove((FILE_NAME+std::to_string(i)+FILE_EXTENSION_NAME).c_str());
}
std::clog<<"[ O K ] clean"<<std::endl;
return true;
}
};
auto main()->int{
short th;
long cycle;
std::string str;
std::cout<<"Thread: ";
std::cin>>th;
std::cout<<"Cycle: ";
std::cin>>cycle;
std::cout<<"String: "<<std::flush;
std::cin.ignore();
getline(std::cin,str);
CDiskFiller df;
df.set(th,cycle,str);
df.run();
std::cout<<"Do you clean up the generated files? (y/n): ";
char c;
std::cin>>c;
if(c=='y'){
df.clean();
}
return 0;
}
总结:什么用都没有(
最后编辑: