我正在寻找一种方法来获取从C++程序中运行命令的输出.我已经看过使用system()函数,但这只会执行一个命令.这是我正在寻找的一个例子:
std::string result = system("./some_command");
Run Code Online (Sandbox Code Playgroud)
我需要运行一个任意命令并获取其输出.我看过Boost.org,但我找不到任何可以满足我需要的东西.
我有一个实用程序可以输出游戏所需的文件列表.如何在C程序中运行该实用程序并获取其输出,以便我可以在同一程序中对其执行操作?
更新:很好地呼吁缺乏信息.该实用程序吐出一系列字符串,这应该是完全可移植的Mac/Windows/Linux.请注意,我正在寻找一种程序化的方法来执行该实用程序并保留其输出(转到stdout).
我正在寻找一个计算我的cuda设备核心数的功能.我知道每个微处理器都有特定的内核,而我的cuda设备有2个微处理器.
我经常搜索一个属性函数来计算每个微处理器的核心数,但我不能.我使用下面的代码,但我还需要核心数量?
码:
void printDevProp(cudaDeviceProp devProp)
{ printf("%s\n", devProp.name);
printf("Major revision number: %d\n", devProp.major);
printf("Minor revision number: %d\n", devProp.minor);
printf("Total global memory: %u", devProp.totalGlobalMem);
printf(" bytes\n");
printf("Number of multiprocessors: %d\n", devProp.multiProcessorCount);
printf("Total amount of shared memory per block: %u\n",devProp.sharedMemPerBlock);
printf("Total registers per block: %d\n", devProp.regsPerBlock);
printf("Warp size: %d\n", devProp.warpSize);
printf("Maximum memory pitch: %u\n", devProp.memPitch);
printf("Total amount of constant memory: %u\n", devProp.totalConstMem);
return;
}
Run Code Online (Sandbox Code Playgroud) #include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main() {
int res = system("ps ax -o pid -o command | grep sudoku | grep gnome > /dev/null");
printf("res = %d \n", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想sudoku通过检查system()(或任何其他调用)的返回代码来查看是否正在运行.我不想在任何地方打印任何输出.
system()即使在查看手册页后,我也不太了解返回代码
无论是否sudoku正在运行,我明白了res = 0.
我正在执行一个system()函数,它返回一个文件名.现在我不想在屏幕上显示输出(即文件名)或管道到新文件.我只想将它存储在一个变量中.那可能吗?如果是这样,怎么样?谢谢
我使用ar.h来定义结构.我想知道如何获取有关文件的信息并将其放入结构中的指定变量中.
struct ar_hdr {
char ar_name[16]; /* name of this member */
char ar_date[12]; /* file mtime */
char ar_uid[6]; /* owner uid; printed as decimal */
char ar_gid[6]; /* owner gid; printed as decimal */
char ar_mode[8]; /* file mode, printed as octal */
char ar_size[10]; /* file size, printed as decimal */
char ar_fmag[2]; /* should contain ARFMAG */
};
Run Code Online (Sandbox Code Playgroud)
使用上面定义的结构,我将如何从文件中获取信息 ls -la
-rw-rw----. 1 clean-unix upg40883 368 Oct 29 15:17 testar
?
我需要编写一个C程序来检查其他程序的输出.它应该基本上像这样工作:
./otherprogram|./myprogram
但我找不到如何从stdout(或管道)逐行读取.然后将所有这些写入stdout.