Skip to content

Instantly share code, notes, and snippets.

@lionello
Created February 23, 2014 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lionello/9166502 to your computer and use it in GitHub Desktop.
Save lionello/9166502 to your computer and use it in GitHub Desktop.
even: skip every other byte in file/stdin
/* 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