I recently started working on this project, and I'm having trouble reading certain things into a global variable. It's for practice with pthreads, which is why I'm using a global variable in the first place. The program is supposed to read in numbers from a file that represent a solved sudoku puzzle, and the text file will be formatted with 9 number characters followed by a new line, nine times. I've made sure that, when running this program, the file is formatted as such. I know that this segment of my code contains the segmentation fault, but I can't tell where. I can only presume that it has something to do with fgets(). However, none of the resources I looked up have anything in them that would make me think that I'm using it incorrectly. It even does this when I resort to using fgetc, reading it in one bit at a time, making accomodations for fgetc returning an int, unlike fgets assigning a string to a variable (in this case, s).
I wouldn't bring it to stack overflow unless I was sure that I couldn't find it; I've been combing over the code for an hour trying to find this seg fault, and it doesn't make any sense to me. I know that the Seg Fault is here because directly after it, it should print out the entire puzzle matrix, but it doesn't make it that far.
int main(int argc, char *argv[]) {
FILE* puzzlefile;
char s[10];
int i=0, j=0, skip;
//open the file passed in via command line
puzzlefile = fopen(argv[1], "r");
for (i=0; i<9; i++){
//get first string of 10 characters
fgets(s,10, puzzlefile);
for (j=0; j<9; i++){
//read the numbers from s into the puzzle 2D
//array, which takes ints. Ignore the 10th
//character, which will be \n
puzzle[j][i] = (int)(s[j]-'0');
}
}
...
}
Your problem seems to be this:
for (j=0; j<9; i++)
^^^
This should be j++
, not i++
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.