I want to compute the checksum of a txt file using C in mac, thus I wrote a simple program as
#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;
it prints the checksum of txt file , but also it prints the path of file. as
30b574b4ddbc681d9e5e6492ae82b32a7923e02e ritesh_file_test.txt
How do I get rid of this path and only access the checksum value?
The output format of
shasum
is of formso, the hash value and the file name are separated by a space. One possible way, to separate the hash from the complete outpuy, is to tokenize
buff
before printing.You can make use of
strtok()
and use space () as delimiter to take out only the checksum value.
That said, in
C
, you don't include#include <iostream>
, don't useusing namespace std;
and don't writecout
. Moreover, use aC
compiler to compileC
code.