File: byte_swap.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (36 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* ----------------------------- MNI Header -----------------------------------
@NAME       : byte_swap.c
@INPUT      : (none)
@OUTPUT     : (none)
@DESCRIPTION: Reads bytes from standard input and writes the 
              swapped bytes standard output.
@METHOD     : 
@GLOBALS    : (none)
@CALLS      : 
@CREATED    : January 13,1991 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>

#define BYTES 2
#define ARRSIZE 800*BYTES
#define VALTYPE char

main()
{
   VALTYPE input[ARRSIZE];
   VALTYPE output[ARRSIZE];
   int i,nread,extra;

   while ((nread = fread(input, sizeof(VALTYPE), 
                         sizeof(input)/sizeof(VALTYPE), stdin)) > 0) {
      if ((extra = nread % BYTES) != 0) nread = nread-extra;
      for (i=0; i < nread; i+=BYTES) {
         output[i] = (VALTYPE) input[i+1];
         output[i+1] = (VALTYPE) input[i];
      }
      (void) fwrite(output, sizeof(VALTYPE), nread, stdout);
   }
   return 0;
}