Jabberwocky submission for Computer Organization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
719 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void btoa ( FILE *ifp, FILE *ofp );
  5. int main( int argc, char *argv[] ) {
  6. FILE *ifp, *ofp;
  7. if (argc < 3) {
  8. fprintf(stderr, "Not enough arguments\n");
  9. exit(1);
  10. }
  11. if (!(ifp = fopen(argv[1],"r"))) {
  12. fprintf(stderr,"Cannot open file %s\n",argv[1]);
  13. exit(1);
  14. }
  15. if (!(ofp = fopen(argv[2],"w"))) {
  16. fprintf(stderr,"Cannot open file %s\n",argv[2]);
  17. exit(1);
  18. }
  19. btoa(ifp, ofp);
  20. return 0;
  21. }
  22. void btoa( FILE *ifp, FILE *ofp ) {
  23. char c;
  24. char byte = 0;
  25. unsigned char mask = 1 << 7;
  26. while ((c = fgetc(ifp)) != EOF) {
  27. if (c == '1') {
  28. byte |= mask;
  29. }
  30. if (!(mask >>= 1)) {
  31. fputc(byte, ofp);
  32. byte = 0;
  33. mask = 1 << 7;
  34. }
  35. }
  36. }