Skip to content

Instantly share code, notes, and snippets.

@lionello
Created Feb 23, 2014
Embed
What would you like to do?
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