I would like to extract file names and their corresponding MD5 sum from a check sum file 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 usinggets().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: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 usingsnprintf()to create the format string: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.