我想在mac中使用C计算txt文件的校验和,因此我编写了一个简单的程序
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
FILE *in;
char buff[512];
if(!(in = popen("shasum ritesh_file_test.txt", "r")))
{
return 1;
}
while(fgets(buff, sizeof(buff), in)!=NULL)
{
cout << buff;
}
printf ("checksum = %s",buff);
pclose(in);
return 0;
它打印txt文件的校验和,还打印文件的路径。作为
30b574b4ddbc681d9e5e6492ae82b32a7923e02e ritesh_file_test.txt
如何摆脱此路径并仅访问校验和值?
最佳答案
shasum
的输出格式是形式
<HASH> <Filename>
所以,哈希值和文件名之间用空格分隔。将哈希值与完整输出分开的一种可能方法是标记 buff
打印前。
您可以使用 strtok()
并使用空格( )作为分隔符,仅取出校验和值。
也就是说,在C
中,您不包括 #include <iostream>
,不要使用using namespace std;
并且不要写cout
。此外,使用 C
编译器编译C
代码。
关于c - 打印txt文件的校验和而不打印文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932790/