I would like to extract file names and their corresponding MD5 sum from a check sum 开发者_如何学Gofile in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.
You can read lines easily enough using fgets()
. Don't even think of using gets()
.
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf()
to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf()
string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf()
to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.
You should be able to implement this with fopen()
, fgets()
and strchr()
- but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a )
character?)
I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?
精彩评论