2011-03-23 131 views
1

我遇到的問題是使用標準輸入從文件中讀取多行整數。這些文件看起來像:C從文件讀取多行文件

123 
423 
235 
523 
..etc 

我此刻的代碼是:

/* 
* Read in the initial puzzle configuration. 
* Each line is 4 characters long: 
* Row as a character '0' .. '9' 
* Column as character '0' .. '9' 
* Digit as character '0' .. '9' 
* Terminating newline. 
* Exits with an error message if there are syntactic 
* or semantic errors with any configuration line. 
*/ 

void configure(FILE *puzzle_file) { 
     int row; 
     int column; 
     int value; 

     while((fscanf(puzzle_file, "%i%i%i\n", row, column, value)) != EOF){ 
       fscanf(puzzle_file, "%i%i%i\n", row, column, value); 
       puzzle[row][column] = value; 
       fixed[row][column] = 1; 
     } 

} 

我試圖用的fscanf因爲該文件的格式是否正確(如根據功能配置上面的評論)但我無法得到它的工作。

如果有不同的,更簡單的方法來解決這個可以看到的解決方案。

語言:C

編輯:

在編譯錯誤:

[email protected]:~/350/sudoku$ make 
gcc -c puzzle.c 
puzzle.c: In function ‘configure’: 
puzzle.c:95: warning: format ‘%i’ expects type ‘int *’, but argument 3 has type ‘int’ 
puzzle.c:95: warning: format ‘%i’ expects type ‘int *’, but argument 4 has type ‘int’ 
puzzle.c:95: warning: format ‘%i’ expects type ‘int *’, but argument 5 has type ‘int’ 
puzzle.c:96: warning: format ‘%i’ expects type ‘int *’, but argument 3 has type ‘int’ 
puzzle.c:96: warning: format ‘%i’ expects type ‘int *’, but argument 4 has type ‘int’ 
puzzle.c:96: warning: format ‘%i’ expects type ‘int *’, but argument 5 has type ‘int’ 
gcc -o sudoku main.o puzzle.o arguments.o 

在我的測試誤差的運行:

[email protected]:~/350/sudoku$ make test_l2 
./sudoku -e p+s/good_puzzle.txt < p+s/script_good_quit.txt 
/bin/sh: line 1: 9143 Segmentation fault  ./sudoku -e p+s/good_puzzle.txt < p+s/script_good_quit.txt 
make: *** [good_configured] Error 139 
+0

這將無法編譯爲拼圖沒有定義 - 你的代碼如何不工作 - 什麼是錯誤信息或其他可見行爲? – Mark 2011-03-23 16:44:24

+0

我將編譯和測試錯誤添加到問題中。 – DestroyerDust 2011-03-23 16:48:23

回答

5

你有兩個問題跟你是什麼做:

首先,您將跳過文件中的一堆行,因爲您在while循環中調用fscanf,然後在檢查循環條件之後立即調用它。您只需要在while循環條件下調用一次。

while((fscanf(puzzle_file, "%i%i%i\n", row, column, value)) != EOF){ 
      // fscanf(puzzle_file, "%i%i%i\n", row, column, value); REMOVE THIS! 
      puzzle[row][column] = value; 
      fixed[row][column] = 1; 
    } 

其次,你會想要讀取每個整數作爲單獨的字符,即。 %c%c%c而不是%i%i%i,並將這些字符代碼轉換爲整數值。即。減((int)ch - 48)其中ch是由fscanf讀入的字符之一。

UPDATE:

您還傳遞了錯誤的值FSCANF,你想通過你的變量不是它們的值的存儲位置。

char row,value,column; 
fscanf(puzzle_file, "%c%c%c\n", &row, &column, &value); 

更新2:

還檢查了ladenedge評論我的答案使用寬度說明符整數值有關,而不是閱讀字符,然後轉換它們。

+1

我相信'%1i%1i%1i'也可以工作,不需要轉換。 – ladenedge 2011-03-23 16:52:18

+0

@ladenedge:哦,我不知道這很好。 – GWW 2011-03-23 16:52:56

+1

@ladenedge:那麼%c%c%c然後將它們轉換爲我需要的字符值,%1i%li%li和它們與int的相同將會做同樣的事情? – DestroyerDust 2011-03-23 16:58:35

1

警告清楚地表明什麼可能出錯在運行時 -

puzzle.c:95: warning: format ‘%i’ expects type ‘int *’, but argument 3 has type ‘int’ 

因此改變

(fscanf(puzzle_file, "%i%i%i\n", row, column, value) 

(fscanf(puzzle_file, "%i%i%i\n", &row, &column, &value) 
// Added & symbol 
0

作爲一個很好的做法,始終使用僞代碼!在上面砌/規格來看,我會做一些事情,如:

  1. 打開文件
  2. 搶第一行(這可能會是一個字符串),而你不是在EOF
  3. 該3位數字分離成單獨的變量
  4. 拼圖[NUM1] [NUM2 = NUM​​3
  5. 固定[NUM1] [NUM2] = 1
  6. 轉到#2

因爲我沒有完全檢查規格,可能會在一兩個地方關閉。