【发布时间】:2014-12-27 16:06:28
【问题描述】:
我试图在 C 程序中使用md5sum 命令,现在我使用dirent.h 来获取文件夹中的所有文件,现在,我想获取所有 md5 所有这些文件,我正在这样做:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
int main(void){
char *word = ".gz";
int i=0;
char *word2 = ".";
char *word3 = "..";
unsigned int md5;
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL)
{
if((strstr(dir->d_name, word) == NULL) && (strcmp(dir->d_name, word2) != 0) && (strcmp(dir->d_name, word3)!= 0)) {
md5 = system("md5sum dir->d_name");
printf("The md5 of %s is %d\n", dir->d_name, md5);
}
}
}
return(0);
}
但是当我运行它时,它会说,例如:
md5sum: dir-: No such file or directory
The md5 of ej1_signal.c is 256
md5sum: dir-: No such file or directory
The md5 of pipeL.c is 256
你能解释一下为什么会这样吗?谢谢!
【问题讨论】:
-
强烈建议先阅读
system函数的文档 -
您是否意识到系统不会返回
md5值,而是命令的退出代码? -
只有我在
"md5sum dir->d_name"上说“wat”吗? -
我强烈建议使用
shell script用于这些目的,通过标准 C 解析/执行 shell 命令是....充其量容易出错,您几乎需要成为专家两种语言。始终使用最适合的工具,即使有人帮助您,其他一切都会让人头疼。 -
尝试使用
snprintf将 "md5sum %s" 与文件的实际名称结合起来......如果您在 Posix 系统上,您可能需要popen()而不是system()。 ..不确定win32替代品