简体   繁体   中英

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.

If there is a different, easier way to solve this solution that would be lovely to see.

Language: 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. You only need to call it once in the while loop condition.

    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. ie. subtract ((int)ch - 48) where ch is one of the characters read in by fscanf.

UPDATE:

You are also passing the wrong values to fscanf, you want to pass the memory location of your variables not their values.

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

UPDATE 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.

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
  3. separate the 3 digits into separate variables
  4. puzzle[num1][num2] = num3
  5. fixed[num1][num2] = 1
  6. Go to #2

Might be off in a place or two since i didn't fully examine the specs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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