【发布时间】:2011-03-23 16:42:54
【问题描述】:
我遇到的问题是使用标准输入从文件中读取多行整数。文件看起来像:
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
编辑:
关于编译错误:
xxxxxxx@linus:~/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
运行我的测试错误:
xxxxxxx@linus:~/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
【问题讨论】:
-
这将无法编译,因为拼图未定义 - 你的代码如何不起作用 - 错误消息或其他可见行为是什么?
-
我在问题中添加了编译和测试错误。