2013-09-22 126 views
-2

我一直想在一個txt文件中的矩陣輸入存儲陣列中,但它告訴我這一點: 這是代碼從文本文件中讀取和二維數組存儲

#include <stdio.h> 

int main() 
{ 
    int c, i, j, row, col, nl, cr; 

    row = col = nl = cr = 0; 

    FILE *fp = fopen("g.txt", "r"); 

    // Figure out how many rows and columns the text file has 
    while ((c = getc(fp)) != EOF) 
    { 
     if (c == '\n') 
      nl++; 
     if (c == '\r') 
      cr++; 

     col++; 

     if (c == '\n') 
      row++; 

     putchar(c); 
    } 

    col = (col - (nl + cr)); 
    col = (int) (col/row); 

    // printf("\nnumber of rows is %d\n", row); 


    // read letters into array 

    char array[row][col]; 

    if (fp) 
     { 
     for (;;) 
       { 
      c = getc(fp); 
       if (c == EOF) 
       { 
         break; 
       } 
       if (c != '\n' && c != '\r') 
       { 
         array[i][j] = c; 

        if (++j >= col) 
         { 
          j = 0; 
          if (++i >= row) 
          { 
           break; 
          } 
         } 
       } 
      } 
     fclose(fp); 
    } 

    for (i = 0; i < row; i++) 
    { 
      for (j = 0; j < col; j++) 
      { 
       putchar(array[i][j]); 
      } 
      putchar('\n'); 
    } 
    return 0; 
} 

有人爲請有任何想法嗎? 爲例TXT文件:

255 50 9 50 1 50 50 1 
50 255 50 50 50 50 50 50 
50 50 255 50 50 50 50 50 
8 50 50 255 50 50 50 50 
50 50 50 50 255 50 50 50 
50 50 50 50 50 255 50 50 
1 50 50 50 50 50 255 50 
2 50 50 50 50 50 50 255 

我的計劃告訴我這一點:

255 50 9 50 1 50 50 1 
50 255 50 50 50 50 50 50 
50 50 255 50 50 50 50 50 
8 50 50 255 50 50 50 50 
50 50 50 50 255 50 50 50 
50 50 50 50 50 255 50 50 
1 50 50 50 50 50 255 50 
2 50 50 50 50 50 50 255  $■(1gÍuáþ09■   ı¤ıu"ÒávD ê$[ 
► ð²( ♥  l ►  ■    
    ê$[ ♥ l  ­²(O»ƒv[ 4■(Qõá 
v♥ #õáv┬²║Oÿ|®v ñ|®ve┬ív 
■( x■(ÿ|®v Ó²⌂    @■( áƒv╚♀[ L 
■(w¯ƒv‼ ê■(I┴ávÿ|®v↓┴áv~²║O 
    Ó²⌂    \■(■   ─ (e┬ívÍ┬29►☺ 

輸入文件顯示它的好,但問題是陣列輸出,我不undrestand爲什麼告訴我這個caracters

+1

其中是您的代碼中的二維數組? –

+0

在一邊注意''double atof(const char * str);'是不需要的,因爲函數已經在stdlib.h中聲明瞭。您的代碼實際上不會使用我的編譯器中的該行進行編譯。 – jpw

+0

雙數[100] [100]; – Butterflay

回答

3

你永遠不會改變j時,會始終保持0,所以你寫的每一個位置上的[I] [0]。的atoi(線);將僅轉換該行中的第一個數字。這就是爲什麼你的程序只存儲第一列。

一種可能的方法來解決這個問題會是這樣的:

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

int main(void) { 
    int i=0,totalNums,totalNum,j=0; 
    size_t count; 
    int numbers[100][100]; 
    char *line = malloc(100); 

    FILE *file; /* declare a FILE pointer */ 
    file = fopen("g.txt", "r"); /* open a text file for reading */ 

    while(getline(&line, &count, file)!=-1) { 
     for (; count > 0; count--, j++) 
      sscanf(line, "%d", &numbers[i][j]); 
     i++; 
    } 

    totalNums = i; 
    totalNum = j; 
    for (i=0 ; i<totalNums ; i++) { 
     for (j=0 ; j<totalNum ; j++) { 
     printf("\n%d", numbers[i][j]); 
     } 
    } 
    fclose(file); 
    return 0; 
} 

該代碼將讀取整行,然後用數字解析它數,直到沒有更多的數字在那裏。

我,如果輸入應該是整數或雙打聽不太懂,請注意您聲明雙打的二維數組,但然後調用的atoi()。我發佈的代碼假設它是整數,但確保將數組更改爲二維數組(如果它們真的是雙精度數據,請在sscanf中更改格式字符串)。

+0

感謝ü烏拉圭回合的答案,我用新的代碼編譯,但我有一個黑色的康壽它什麼都不顯示:( – Butterflay

+0

@Butterflay你說得對,對不起, sscanf()最終因爲換行符而被阻塞我編輯了我的答案,現在代碼正常工作,我測試了它。 –

+0

soryy我有錯誤getline未定義 – Butterflay

1
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

using namespace std; 

int main(void) 
{ 
    FILE * fp; 
    char * line = NULL; 
    size_t len = 0; 
    ssize_t read; 

    const char *s = " "; 
    char *token = NULL; 

    int i = 0; 
    double arr[200]; 
    int j; 

    fp = fopen("g.txt", "r"); 
    if (fp == NULL) 
    { 
    printf("Error opening"); 
    exit(EXIT_FAILURE); 
    } 

    while ((read=getline(&line, &len, fp)) != -1) 
    { 
    token = strtok(line, s); 

    while(token != NULL) 
    { 
     arr[i] = atoi(token); 
     printf("%f\n", arr[i]); 
     token=strtok(NULL,s); 
     i++; 
    } 
    } 

    exit(EXIT_SUCCESS); 
    return 0; 
} 

getline將由線讀取文件中的行和strtok將分裂基於空間條目和將seperately條目存儲在數組中。單維數組也足以存儲值。

+0

感謝ü但有許多錯誤後,編譯它 – Butterflay

+0

@Butterflay,你包括頭文件?同時運行它作爲一個單獨的新節目 – user1502952

+0

是的,當然我添加 – Butterflay

相關問題