[英]Error checking for empty directory in C++
I'm trying to create a folder named by todays date (on Ubuntu) and then check if it's empty or not.我正在尝试创建一个以今天日期命名的文件夹(在 Ubuntu 上),然后检查它是否为空。
The empty-or-not check will be done several times daily.空或不检查将每天进行数次。
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <typeinfo>
#include <chrono>
#include <time.h>
#include <iomanip>
using namespace std;
int main() {
//Pull out system date and create a folder named by system date
auto const now = std::chrono::system_clock::now();
auto const in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%d_%m_%Y");
// Creating todays date folder with entry folder
string str_2=std::string("mkdir -p " + string(ss.str()) + "/entry");
const char *com2=str_2.c_str();
system(com2);
//check if directory is empty or not
int check;
char is_empty[100];
FILE * output;
output = popen("ls " + ss.str() + "/entry | wc -l","r") ;
fgets (is_empty, 100, output); //write to the char
pclose (output);
check = atoi(is_empty);
if (check == 0) {
cout << "The folder is empty" << endl;
}
}
I'm getting this error when compiling this code:编译此代码时出现此错误:
error: no match for ‘operator+’ (operand types are ‘const char [4]’
and ‘std::stringstream {aka std::__cxx11::basic_stringstream<char>}’)
output = popen("ls " +ss+ "/entry | wc -l","r") ;
This solved it for me这为我解决了
int check;
char is_empty[100];
FILE * output;
string cmd = std::string("ls " + string(ss.str())+ "/entry | wc -l") ;
const char *com4 = cmd.c_str();
output = popen(com4,"r");
fgets(is_empty, 100, output);
pclose (output);
check = atoi(is_empty);
//cout << check<<endl;
if (check == 0){
cout << "The folder is empty" << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.