I’m trying to scan in a txt file with a string of numbers.

t.txt
123
223
323

File *f = fopen(“t.txt”, “r”)

while (i != 3){
for (int j = 0; j < 3; j++){
fscanf(f, “%1c”, &grid[i][j]);
}
i++;
}

But it prints a segment fault.

Recommended Answers

All 2 Replies

So much left unwritten. I don't see how grid was declared, the value i might not be initialized and there are usually some references to which C library will be used in the first few lines. I'll write incomplete code means I would be guessing here.

commented: I isolated the problem to these few lines, and initialized char grid[3][3], along with the other vars. +0

There isn't enough code for me to compile even with that. I suggest you single step the code in the usual debugger to see why it fails.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.