简体   繁体   English

C从文件中读取多行

[英]C reading in multiple lines from file

The problem I have is reading in multiple lines of integers from a file using standard input. 我的问题是使用标准输入从文件中读取多行整数。 The files looks like: 这些文件如下所示:

123
423
235
523
..etc

The code I have at the moment is: 我目前的代码是:

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

}

I was trying to use fscanf because the file is formatted correctly (as according to the comment above the function configure) but I couldn't get it working. 我正在尝试使用fscanf,因为文件格式正确(根据配置函数上方的注释),但是我无法使其正常工作。

If there is a different, easier way to solve this solution that would be lovely to see. 如果有另一种更轻松的方法来解决此解决方案,那将很有趣。

Language: C 语言:C

Edit: 编辑:

On compile error: 关于编译错误:

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

On run of my test error: 运行我的测试错误:

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

You have two issues with what you are doing: 您在做什么有两个问题:

First you are going to skip a bunch of lines in your file because you are calling fscanf in the while loop and then right after the loop condition is checked. 首先,您将跳过文件中的许多行,因为在while循环中调用fscanf,然后在检查循环条件之后立即执行。 You only need to call it once in the while loop condition. 您只需要在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;
    }

Second, you would want to read each of those integers as separate characters, ie. 其次,您需要将每个整数读取为单独的字符,即。 %c%c%c instead of %i%i%i , and convert those characters codes to integer values. %c%c%c而不是%i%i%i ,然后将这些字符代码转换为整数值。 ie. 即。 subtract ((int)ch - 48) where ch is one of the characters read in by fscanf. 减去((int)ch-48),其中ch是fscanf读取的字符之一。

UPDATE: 更新:

You are also passing the wrong values to fscanf, you want to pass the memory location of your variables not their values. 您还将错误的值传递给fscanf,想要传递变量的内存位置而不是其值。

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

UPDATE 2: 更新2:

Also check out ladenedge comment to my answer regarding using a width specifier for the integer values, instead of reading in characters and converting them. 还要检查关于我的答案的ladenedge评论,该评论关于对整数值使用宽度说明符,而不是读取字符并进行转换。

The warning clearly suggest what might go wrong at run time - 该警告明确表明在运行时可能出了什么问题-

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

So change 所以改变

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

to

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

As a good practice, always use pseudo-code! 作为一个好习惯,请始终使用伪代码! Looking at the puzzle/spec above, I'd do something like: 看看上面的难题/规格,我会做类似的事情:

  1. open the file 打开文件
  2. grab the first line (it'll probably be a string) while you're not at EOF 当您不在EOF时,抓住第一行(可能是字符串)
  3. separate the 3 digits into separate variables 将3位数字分成单独的变量
  4. puzzle[num1][num2] = num3 拼图[num1] [num2] = num3
  5. fixed[num1][num2] = 1 固定的[num1] [num2] = 1
  6. Go to #2 转到#2

Might be off in a place or two since i didn't fully examine the specs. 由于我没有完全检查规格,所以可能在一两个地方放过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM