I'm new to C and programming as a whole. I've got the basics down for a program I'm trying to get a better grasp of C, but I'm having trouble taking input from the users args at the command line to populate my array:
./sudoku.c "9...7...." "2...9..53" etc etc
I've tested my program using an array that I filled in and it works but that is no good if I can't take user input. My input looks something like this:
grid[9][9] = {{9, 0, 0, 0, 7, 0, 0, 0, 0},
{2, 0, 0, 0, 9, 0, 0, 5, 3}};
Any suggestions?
Any help would be greatly appreciated
Just read with simple loops.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char* argv[]) {
int grid[9][9];
int input_error = 0;
int i, j;
if (argc != 1 + 9) { /* check number of rows in input */
input_error = 1;
} else {
for (i = 0; i < 9; i++) { /* read each rows */
if (strlen(argv[i + 1]) != 9) { /* check number of cols in input */
input_error = 1;
break;
}
for (j = 0; j < 9; j++) { /* read each cols */
if (isdigit(argv[i + 1][j]) && argv[i + 1][j] != '0') {
/* digits except for 0 */
grid[i][j] = argv[i + 1][j] - '0'; /* convert digit to integer */
} else if (argv[i + 1][j] == '.') {
/* dot */
grid[i][j] = 0;
} else {
/* invalid character */
input_error = 1;
break;
}
}
}
}
/* check if some errors are detected */
if (input_error) {
fputs("invalid usage\n", stderr);
return 1;
}
/* print what is read for testing */
for (i = 0; i < 9; i++) {
for(j = 0; j < 9; j++) {
printf(" %d", grid[i][j]);
}
putchar('\n');
}
return 0;
}
Consider using text file as an input, and give filename as command line argument.
Example:
#include <stdio.h> #include <ctype.h> int main(int argc, char* argv[]) { int lineCnt; int posCnt; FILE* inpFile = NULL; // check argument and file if( argc < 2 ) { printf("run program with argument - name of file\\n"); return 1; } inpFile = fopen(argv[1], "r"); if( inpFile == NULL ) { printf("file %s cannot be open\\n", argv[1]); return 2; } // reading from file int grid[9][9] = {0}; int chr; for( lineCnt = 0; lineCnt < 9; lineCnt++) { for ( posCnt = 0; posCnt < 9; posCnt++) { // read next not sapce character from file do{ chr = getc(inpFile); } while( isspace(chr) ); // check character if( chr == EOF ) // file is finished but array is not filled { printf("file %s if incomplete\\n", argv[1]); return 3; } if(chr == '.') // if(chr == '.' || chr == '0') { grid[lineCnt][posCnt] = 0; } else if(chr > '0' && chr <= '9') { grid[lineCnt][posCnt] = chr - '0'; } else { printf("file %s has incorrect format\\n", argv[1]); return 4; } } } // test output of input data for( lineCnt = 0; lineCnt < 9; lineCnt++) { for ( posCnt = 0; posCnt < 9; posCnt++) { printf("%i ", grid[lineCnt][posCnt]); } printf("\\n"); } }
If you compine this example and run as
./sudoku.c sudoku1.txt
where sudoku1.txt
is like
1...5.7..
...3.5.91
.5.6.7.87
11111....
....2222.
6.6.6.6.6
.1.1.1.1.
1.1.1.1.1
.9.9.9.99
you will understand my idea
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.