Created
February 23, 2014 03:39
-
-
Save lionello/9166502 to your computer and use it in GitHub Desktop.
even: skip every other byte in file/stdin
This file contains 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
/* even, by Lionello Lunesu, placed in the public domain */ | |
#include <stdio.h> | |
int even(FILE * f) | |
{ | |
char buf[4096]; | |
int r, read, wrote; | |
while (1) | |
{ | |
read = fread(buf, 1, sizeof(buf), f); | |
for (r=1; r<read/2; ++r) | |
buf[r] = buf[r*2]; | |
wrote = fwrite(buf, read/2, 1, stdout); | |
if (wrote != 1) | |
return 1; | |
if (read != sizeof(buf)) | |
break; | |
} | |
return 0; | |
} | |
int main(int argc, char** argv) | |
{ | |
int i, r = 0; | |
if (argc == 1 || (argc == 2 && argv[1][0] == '-' && !argv[1][1])) | |
return even(stdin); | |
for (i=1; i<argc; ++i) | |
{ | |
FILE * f = fopen(argv[i], "r"); | |
if (f) | |
{ | |
r = even(f); | |
fclose(f); | |
if (r) | |
break; | |
} | |
else | |
{ | |
r = 2; | |
} | |
} | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment