我正在执行一个system()函数,它会返回一个文件名给我。现在我不想把输出显示到屏幕上(也就是文件名),也不想将其导入到一个新文件中,我只想将其存储在变量中。这是否可能?如果可能,该怎么做? 谢谢
system()
函数。popen()
函数。它在C和C++中都可用,您已将问题标记为两者之一,但可能会编写其中之一。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fpipe;
char *command = "ls";
char c = 0;
if (0 == (fpipe = (FILE*)popen(command, "r")))
{
perror("popen() failed.");
exit(EXIT_FAILURE);
}
while (fread(&c, sizeof c, 1, fpipe))
{
printf("%c", c);
}
pclose(fpipe);
return EXIT_SUCCESS;
}
fread()
未对存储在line
中的字符串进行空终止,因此这将导致在printf
调用时出现未定义的行为。 - Mark Lakata嗯,还有一种简单的方法,可以将命令输出存储到文件中,这种方法称为重定向。我认为重定向非常容易,对你来说会很有用。
例如,这是我的c++代码:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
system("ls -l >> a.text");
return 0;
}
popen(3)
函数,并从该文件中读取内容。FILE *popen(const char *command, const char *type);
基本上,您运行command
,然后从返回的FILE
中读取。 popen(3)的工作方式与system相同(调用shell),因此您应该能够使用它运行任何内容。
这是我的C++实现,它将system()
的标准输出重定向到一个日志系统。它使用GNU libc的getline()
。如果无法运行命令,它会抛出异常,但如果命令以非零状态运行,则不会抛出异常。
void infoLogger(const std::string& line); // DIY logger.
int LoggedSystem(const string& prefix, const string& cmd)
{
infoLogger(cmd);
FILE* fpipe = popen(cmd.c_str(), "r");
if (fpipe == NULL)
throw std::runtime_error(string("Can't run ") + cmd);
char* lineptr;
size_t n;
ssize_t s;
do {
lineptr = NULL;
s = getline(&lineptr, &n, fpipe);
if (s > 0 && lineptr != NULL) {
if (lineptr[s - 1] == '\n')
lineptr[--s ] = 0;
if (lineptr[s - 1] == '\r')
lineptr[--s ] = 0;
infoLogger(prefix + lineptr);
}
if (lineptr != NULL)
free(lineptr);
} while (s > 0);
int status = pclose(fpipe);
infoLogger(String::Format("Status:%d", status));
return status;
}
system()
中,您要执行哪个命令? - Nawaz