1. linux下c调用shell命令的方法
方法:使用 popen() 函数 ,需要包含头文件 #include <stdio.h>
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
FILE *fp;
fp = popen("ls /home", "w");
if (NULL == fp) {
cout << "error" << endl;
}
pclose(fp);
return 0;
}
2. 检查文件是否存在
方法: 使用access() 函数,需要包含头文件 #include <unistd.h>
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
using namespace std;
int main(void)
{
if (access("/home/1.txt", F_OK) == -1) {
cout << "不存在" << endl;
}
return 0;
}
access() 函数:
存在,返回0 ;
不存在,返回-1
---