password为sudo的密码
#include "include/rapidjson/stringbuffer.h" #include "include/rapidjson/writer.h" #include "include/rapidjson/document.h" #include <iostream> #include <string> #include <eigen3/Eigen/Dense> #include <vector> #include <ctime> #include <fstream> #include <dirent.h> #include <unistd.h> // using namespace std; int main() { std::string datadir = "/home/test1/test1/ste1"; if (access(datadir.c_str(), R_OK) == 0) { std::string command1="echo password | sudo -S touch "+datadir+"/12345.json"; int result1= system(command1.c_str()); std::string command2="echo password | sudo -S chmod a+rwx "+datadir+"/12345.json"; int value3=system(command2.c_str()); std::ofstream outfile1; outfile1.open(datadir+"/12345.json"); outfile1<<"helloworld"<<std::endl; outfile1.close(); std::cout << "dir exsist" << std::endl; } else { std::string command="echo password | sudo -S mkdir -p "+datadir; int result= system(command.c_str()); std::string command1="echo password | sudo -S touch "+datadir+"/123.json"; int result1= system(command1.c_str()); } return 0; }
标签:std,int,代码,sudo,写入,json,datadir,文件夹,include From: https://www.cnblogs.com/gishuanhuan/p/17598468.html