打印到输出文件时出现总线错误

时间:2014-02-27 05:48:48

标签: c printf bus-error

尝试写入输出文件时,我一直收到总线错误。我的二进制函数将输入文本从输入文件转换为二进制文件并将其放入输出文件中。但是当我跑步时,我一直收到这个总线错误,我不知道为什么!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define MAXLEN 255
#define FLAG 1
#define INPUT 2
#define OUTPUT 3

int binary(unsigned char x){ //, int argc, char* argv[]){
  FILE* out;

  unsigned char i, n = (sizeof(x) * CHAR_BIT) -1;
  int t;
  for(i=0; i<= n; i++){

    fprintf(out, "%d",(x >> (n-i) & 1));
  }
  fprintf(out, " \n");
}

int main(int argc, char *argv[]){

  FILE *in, *out;
  char instring[MAXLEN];
  const char *p;

  in = fopen(argv[INPUT], "r");
  if(in == NULL){
    printf("Error: This File is Empty.\n");
    exit(1);
  }else{
     out = fopen(argv[OUTPUT], "w");

  if(strcmp(argv[FLAG], "-b") == 0){
    fgets(instring, MAXLEN, in);

    for(p = instring; *p != '\0'; p++){
      // fprintf(out, "%d", binary(*p));
      binary(*p);
  }
 }
   else
   if(strcmp(argv[FLAG], "-t") == 0){

  }

   else{
    printf("Error: Flag Not Recognized.\n");
    return 0;
   }
 }
  fclose(in);
  fclose(out);
}

0 个答案:

没有答案