Created
September 19, 2013 10:55
-
-
Save motiejus/6621835 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Byte calculator. Sums bytes of a given file and displays to screen. | |
| * | |
| * Compile: | |
| * gcc calc.c -o calc -O2 | |
| * | |
| */ | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <stdint.h> | |
| #define BUFFER (1 << 20) | |
| unsigned long long do_calc(FILE *f) { | |
| uint8_t buf[BUFFER]; | |
| unsigned long long sum = 0; | |
| size_t nbytes, i; | |
| while ((nbytes = fread(buf, 1, BUFFER, f)) > 0) | |
| for (i = 0; i < nbytes; i++) | |
| sum += buf[i]; | |
| return sum; | |
| } | |
| int main(int argc, const char* argv[]) { | |
| FILE *f; | |
| unsigned long long res; | |
| if (argc != 2) | |
| return 1; | |
| if ((f = fopen(argv[1], "rb")) == NULL) { | |
| perror("fopen"); | |
| return 1; | |
| }; | |
| res = do_calc(f); | |
| fclose(f); | |
| printf("%llu\n", res); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Best I can do for now: https://gist.github.com/yfyf/6639162