Skip to content

Instantly share code, notes, and snippets.

@motiejus
Created September 19, 2013 10:55
Show Gist options
  • Select an option

  • Save motiejus/6621835 to your computer and use it in GitHub Desktop.

Select an option

Save motiejus/6621835 to your computer and use it in GitHub Desktop.
/*
* 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;
}
@yfyf
Copy link

yfyf commented Sep 20, 2013

Best I can do for now: https://gist.github.com/yfyf/6639162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment