2016-06-21 13 views
-1

所以我知道這是一個很長的標題,但我會盡量分解它。從文件讀取多個整數,將它們轉換爲二進制文件,然後將它們打印到屏幕上的指導?

我想創建一個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; 

} 

我真的不知道如何有效地讀取數字並將它們逐一轉換。我已經嘗試了一個for循環,如下所示,包含10個不同整數的測試文件(我已經聲明瞭該文件並使用必要的File 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; 

} 

任何幫助深表感謝!

+0

什麼是你的fscanf後得到的結果?嘗試在scanf之後添加printf以顯示您讀取的值。並驗證您的文件的結構。 – Mimouni

+1

發佈整個有問題的'main'而不是描述它將有助於避免「你做了這件事」和「你做了那件事」這麼長的序列。 – molbdnilo

+0

你發佈的代碼工作正常。而不是描述_doesn't_ work發佈的代碼。 –

回答

0

一些建議:

  1. 使用int64_t#include<stdint.h>而不是long int
  2. 檢查錯誤,是scanf成功與否:

    if (scanf("%ld", &integerInput) != 1){ 
        printf("incorrect input.\n"); 
        return 1; // error 
    } 
    
  3. 您可以使用OS管/重定向標準輸入或文件:https://en.wikipedia.org/wiki/Redirection_(computing)
    所以你可以使用scanf既爲用戶輸入(stdin)和文件輸入(os管道)。
    這簡單你的代碼。

所以剩下很簡單:
使用環路,則內循環:讀取輸入與scanf然後轉換爲二進制然後printf

  • 可以使用itoa#include<stdlib.h>爲整數轉換爲字符串:

    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; 
    } 
    

    ,並調用它,如:文件1 < FILE2.TXT

    ,或者您可以使用fscanf如果你想。
    我希望這可以幫助。

    0

    一個通過輸入文件簡單的方法來循環是:

    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); 
    } 
    
    相關問題