打印语句更改char数组输出

时间:2016-01-24 02:24:20

标签: c arrays char printf

我试图将一些文本(逐个字符)转换为二进制表示。由于某种原因,print语句printf("Hold is %d or %c: ", hold, hold);正在改变我的函数的输出,我不知道如何解释它。任何帮助将不胜感激。测试文件只是一个文本文件,里面有Hello, World!

用它:

Hold is 72 or H: 01001000
Hold is 101 or e: 01100101
Hold is 108 or l: 01101100
Hold is 108 or l: 01101100
Hold is 111 or o: 01101111
Hold is 44 or ,: 00101100
Hold is 32 or  : 00100000
Hold is 87 or W: 01010111
Hold is 111 or o: 01101111
Hold is 114 or r: 01110010
Hold is 108 or l: 01101100
Hold is 100 or d: 01100100
Hold is 33 or !: 00100001

没有它:

1000 �
0101 �
1100 �
1100 �
1111 �
1100 �
0000 �
0111 �
1111 �
0010 �
1100 �
0100 �
0001 �

代码

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

void decimal_to_binary(unsigned long num, FILE *out) {    
    int i = 255, a = 0;
    char binarr[255];
    for (i = 0; i < 255; i++) { binarr[i] = '0'; }
    if (num != 0) {
        while (num != 0) {
            if (num % 2 == 0) {
                binarr[i] = '0';
                i--;
            } else {
                binarr[i] = '1';
                i--;
            }
            num /= 2;
        }
    } else {
        fprintf(out, "00000000");
    }
    fprintf(out, "%s ", binarr + strlen(binarr) - 8);
    printf("%s\n", binarr + strlen(binarr) - 8);
    memset(binarr, 0, sizeof(binarr));    
}

int main(int argc, char *argv[]) {
    int hold;
    FILE *in = fopen(argv[1], "r");
    FILE *out = fopen(argv[2], "w+");

    while (!feof(in)) {
        hold = fgetc(in);
        if (hold > 0 && hold != 10){
            printf("Hold is %d or %c: ", hold, hold);
            decimal_to_binary(hold, out);
        }
    }
    fclose(in);
    fclose(out);
    return 0;
}

3 个答案:

答案 0 :(得分:1)

您的decimal_to_binary功能不正确:

  • 您的索引超出了binarr数组的结尾。
  • 您不会终止此数组以将其传递给printf

这是一个更简单和更正的版本:

void decimal_to_binary(unsigned long num, FILE *out) {
    int i = 256, a = 0;
    char binarr[257];
    memset(binarr, '0', sizeof(binarr) - 1);
    binarr[i] = '\0';
    while (num != 0) {
        --i;
        if (num % 2) {
            binarr[i] = '1';
        }
        num /= 2;
    }
    if (i > 256 - 8) // print at least 8 bits
        i = 256 - 8;
    fprintf(out, "%s ", binarr + i);
    printf("%s\n", binarr + i);
}

您的功能main也存在问题:

  • 您使用feof(in)测试文件结尾。这是不正确的,您应该检查holdEOF
  • '\n'的值硬编码为10是不好的做法。

这是一个正确的版本:

int main(int argc, char *argv[]) {
    int hold;
    FILE *in = fopen(argv[1], "r");
    FILE *out = fopen(argv[2], "w+");

    while ((hold = fgetc(in)) != EOF) {
        if (hold != '\n') {
            printf("Hold is %d or %c: ", hold, hold);
            decimal_to_binary(hold, out);
        }
    }
    fclose(in);
    fclose(out);
    return 0;
}

答案 1 :(得分:1)

我减少了极大的数组,确保用空字符终止字符串,将数组归零,然后使用fprintf打印它。这解决了这个问题。

void decimal_to_binary(unsigned long num, FILE *out){

    int i = 7, a = 0;
    char binarr[9];
    binarr[8]='\0';
    for (a=7; a>=0; a--){ binarr[a] = '0'; }
    if (num != 0) {
        while (num!=0){
            if (num%2 == 0){
                binarr[i] = '0';
                i--;
            }
            else { binarr[i] = '1'; i--; }
            num /= 2;
        }
    } else { fprintf(out, "00000000"); }
    fprintf(out, "%s ", binarr);
    memset(binarr, 0, sizeof(binarr));
}

答案 2 :(得分:1)

由于几个原因,您的程序有不确定的行为。

  1. 您没有空终止字符串。在这样的字符串上调用strlen会导致未定义的行为。
  2. 您正在使用越界索引修改binarr。这也是未定义行为的原因。
  3. 我对您的函数decimal_to_binary进行了注释,指出了这些错误的位置。

    void decimal_to_binary(unsigned long num, FILE *out){
    
        int i = 255, a = 0;
        char binarr[255];
        for (i=0; i<255; i++){ binarr[i] = '0'; }
    
        // All the elements of binarr are set to '0'.
        // It's not a null terminated string.
    
        if (num != 0) {
            while (num!=0){
    
               // The value of i is 255 when this loop is 
               // entered the first time.
               // Setting the value of binarr[255] is cause for
               // undefined behavior.
    
                if (num%2 == 0){
    
                    binarr[i] = '0';
                    i--;
                }
                else { binarr[i] = '1'; i--; }
                num /= 2;
            }
        } else { fprintf(out, "00000000"); }
        fprintf(out, "%s ", binarr + strlen(binarr) - 8);
        printf("%s\n", binarr + strlen(binarr) - 8);
        memset(binarr, 0, sizeof(binarr));
    }
    

    修复很简单。

    1. 使用空字符终止字符串。

      for (i=0; i<255; i++){ binarr[i] = '0'; }
      i--;
      binarr[i] = '\0';
      
    2. binarr循环中修改while时使用正确的索引。

      while (num!=0){
      
          // Decrement the index before you assign to the next element.
          // When the loop is entered the first time, i = 254, which
          // is used to null terminate binarray.
          // The next '1' or '0' needs to be placed at i = 253.
          i--;
      
          if (num%2 == 0){
      
              binarr[i] = '0';
          }
          else {
             binarr[i] = '1';
          }
          num /= 2;
      }