linux 调用 文件函数调用,Linux文件系统函数调用案例

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

vector split_str(string s);

class fileOperate

{

public:

int md(string name){

mode_t mode = 0775;

if(mkdir(name.c_str(), mode)==0)

{

cout<

return 0;

}

else

{

cout<

return -1;

}

}

int del(string fileName){

if(remove(fileName.c_str()) == 0 )

{

cout<

return 0;

}

else

{

cout<

return -1;

}

}

int cd(string path){

return chdir(path.c_str())==0? 0 : -1;

}

void ls(){

//列出文件夹内容

DIR * curDir;

struct dirent *ent;

struct stat entStat;

if((curDir=opendir("."))==NULL)

{

cout<

}

else

{

while((ent=readdir(curDir))!=NULL)//遍历该文件目录下的内容

{

if(stat(ent->d_name, &entStat) == 0)

{

if(entStat.st_mode & S_IFDIR)//判断是否是文件夹

{

cout<

cout<

cout<

cout<

cout<d_name<

}

else if(entStat.st_mode & S_IFREG)

{

cout<

cout<

cout<

cout<

cout<d_name<

}

}

}

closedir(curDir);

}

}

int cp(string oldfile,string oldfilePath,string newfilePath){//深度拷贝

struct dirent *ent;

struct stat entStat,entStat2;

cd(oldfilePath);

if( stat(oldfile.c_str(),&entStat)==0){

if(entStat.st_mode & S_IFDIR){

cd(newfilePath);

if(md(oldfile)==0){

DIR * curDir;

cd(oldfilePath);

cd(oldfile);

curDir=opendir(".");

while((ent=readdir(curDir))!=NULL)

{

if(stat(ent->d_name, &entStat2) == 0)

{

if(ent->d_name[0]!='.'){//跳过本身和复文件

string newpath=newfilePath+'/'+oldfile;

string oldpath=oldfilePath+'/'+oldfile;

cp(ent->d_name,oldpath,newpath);

cd(oldfilePath);

cd(oldfile);

}

}

}

closedir(curDir);

cd("..");

}else

return -1;

}else{

cd(oldfilePath);

ifstream file(oldfile.c_str(),ios::binary);

if(!file){

cout<

return -1;

}

cd(newfilePath);

ofstream file2(oldfile.c_str(),ios::binary);

if(!file2){

cout<

return -1;

}

file2<

file.close();

file2.close();

return 0;

}

}

return 0;

}

};

vector split_str(string s)//处理输入指令

{

vector tokens;

istringstream iss(s);//以空格为分割符号,把该行分割开

do

{

string sub;

iss >> sub;

tokens.push_back(sub);

} while (iss);

tokens.erase(tokens.end()-1); // 删除最后的换行回车符

return tokens;

}

int main()

{

fileOperate operate;

cout<

cout<

cout<

cout<

cout<

cout<

string commandLine;

vector commandArgs;

getline(cin,commandLine);

commandArgs=split_str(commandLine);

while(commandArgs.size()>0 && "q"!=commandArgs[0])//按回车或者输入q都能退出程序

{

if(commandArgs[0]=="cd"){

if(commandArgs.size()<2)

cout<

else{

if(operate.cd(commandArgs[1])==0){

cout<

}else

cout<

}

}else if(commandArgs[0]=="md"){

if(commandArgs.size()<2)

cout<

else

operate.md(commandArgs[1]);

}else if(commandArgs[0]=="ls"){

operate.ls();

}else if(commandArgs[0]=="del"){

if(commandArgs.size()<2){

cout<

}else{

cout<

if(operate.del(commandArgs[1])==0){

cout<

}else{

cout<

}

}

}else if(commandArgs[0]=="cp"){

if(commandArgs.size()!=4){

cout<

}else{

if(operate.cp(commandArgs[1],commandArgs[2],commandArgs[3])==0)

cout<

else

cout<

}

}

cout<

getline(cin,commandLine);

commandArgs=split_str(commandLine);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值