c - printing the checksum of a txt file without printing the file path -
i want compute checksum of txt file using c in mac, wrote simple program
#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 checksum of txt file , prints path of file. as
30b574b4ddbc681d9e5e6492ae82b32a7923e02e ritesh_file_test.txt
how rid of path , access checksum value?
the output format of shasum of form
<hash> <filename> so, hash value , file name separated space. 1 possible way, separate hash complete outpuy, tokenize buff before printing.
you can make use of strtok() , use space () delimiter take out checksum value.
that said, in c, don't include #include <iostream>, don't use using namespace std; , don't write cout. moreover, use c compiler compile c code.
Comments
Post a Comment