【问题标题】:Guidance on reading multiple integers from file, converting them to binary, then printing them to screen?从文件中读取多个整数,将它们转换为二进制,然后将它们打印到屏幕上的指南?
【发布时间】:2016-10-22 18:49:37
【问题描述】:

所以我知道这是一个很长的标题,但我会试着把它分解一下。

我正在尝试创建一个 C 程序,它将在此之前从 .txt 文件中读取不确定数量的整数(假设该文件仅包含整数,并且每个整数由一个新行分隔)将每一个转换为二进制形式,然后将其导出到屏幕上。

我正在尝试逐步创建程序,到目前为止,我已经设法创建了一个转换为二进制的程序,但只能从单个用户输入的整数(参见下面的代码)。

#include<stdio.h>

int main(){

long int integerInput,quotient;

int binaryNumber[100],i=1,j;


printf("Enter any integer: ");

scanf("%ld",&integerInput);


quotient = integerInput;


while(quotient!=0){

     binaryNumber[i++]= quotient % 2;

     quotient = quotient / 2;

}


printf("Equivalent binary value of integer %d: ",integerInput);

for(j = i -1 ;j> 0;j--)

     printf("%d",binaryNumber[j]);


return 0;

}

我真的不确定如何有效地读取数字并一一转换。我在包含 10 个不同整数的测试文件上尝试了如下所示的 for 循环(我已声明此文件并使用必要的文件 I/O 打开它)。

我尝试使用 for 循环编辑上述代码,但这似乎没有帮助 - 它只是生成随机序列。

for(k=0;k<10;k++)
{
    fscanf(test, "%d", &decimalNumber);
.... // rest of above code is inserted here, minus the scanf and prompts for user to enter a number
return 0;

}

非常感谢任何帮助!

【问题讨论】:

  • 在 fscanf 之后得到的结果是什么?尝试在 scanf 之后添加 printf 以显示您读取的值。并验证文件的结构。
  • 张贴整个有问题的main而不是描述它有助于避免一连串的“你做了这个”和“你做了那个”。
  • 您发布的代码运行良好。而不是描述不起作用的代码发布它。
  • while循环应该是do...while循环,否则0的值不会被转换。
  • binaryNumber 数组应初始化为零,因此如果您希望填充到特定宽度,也可以轻松显示前导零,这对于二进制表示很常见。此外,第一个元素被浪费了,因为索引从i=1 开始。因为ij 是有符号类型,所以从0 开始索引并允许在下降的for 循环的终止条件中等于0。并显示工作的完整代码!

标签: c binary integer


【解决方案1】:

一些建议:

  1. 使用来自#include&lt;stdint.h&gt;int64_t 而不是long int
  2. 检查错误,scanf 是否成功:

    if (scanf("%ld", &integerInput) != 1){
        printf("incorrect input.\n");
        return 1; // error
    }  
    
  3. 您可以使用操作系统管道/重定向标准输入或文件:https://en.wikipedia.org/wiki/Redirection_(computing)
    因此您可以将scanf 用于用户输入(标准输入)和文件输入(操作系统管道)。
    这简化了您的代码。

剩下的就很简单了:
使用循环然后在循环内:使用scanf 读取输入,然后转换为二进制然后printf

  1. 您可以使用itoa 来自#include&lt;stdlib.h&gt; 将整数转换为字符串:

    int32_t n = 1234567890;
    char buf[65];
    itoa(n, buf, 2);
    printf("%s\n", buf);  // 1001001100101100000001011010010
    

并放在一起,
工作示例代码:

#include<stdio.h>
#include<stdint.h>
#include<stdlib.h> 

int main(){
    int32_t n = 1234567890;
    char buf[65];
    while (1){
        if (scanf("%ld", &n) != 1){
            //printf("incorrect input\n"); // or EOF
            return 1; // error or EOF
        }
        itoa(n, buf, 2);
        printf("%s\n", buf);  // 1001001100101100000001011010010 
    }
    return 0;
}

并称它为:file1

如果您愿意,也可以使用fscanf
我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    循环输入文件的一种简单方法是:

    while( fscanf( fp, "%ld", &inputInteger ) == 1 )
    {
      // convert and display inputInteger
    }
    

    fp 是您的输入流。 *scanf 返回成功转换和赋值的次数;因为我们只读取一个项目,所以我们希望成功时返回值1。这将循环直到 EOF 或 fscanf 看到一个非空白、非十进制数字字符。

    您可以使用可选的命令行参数从文件或标准输入中读取程序:

    int main( int argc, char **argv )
    {
      FILE *fp = stdin; // default to standard input
      if ( argc > 1 )
      {
        if ( !(fp = fopen( argv[1], "r" )) )
        {
          fprintf( stderr, "Could not open %s\n", argv[1] );
          return EXIT_FAILURE;
        }
      }
    
      while ( fscanf( fp, "%ld", &inputInteger ) == 1 )
      {
        // convert and display integer
      }
    
      if ( fp != stdin )
        fclose( fp );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      相关资源
      最近更新 更多