Hello. I am making a program in c from the linux nano complier. I made and tested the program in Dev C++ and it works perfect. When I put it in nano, it complies, but when I run it, it gives a segmentation fault. Please tell me how to fix it. Thanks.
This is what I am doing:

1. I put the file to read on my desktop.
2. I code using nano mycode.c
3. I compile with cc -c mycode mycode.c

It compiles fine, but when I run it (./mycode) it gives me segmentation fault.

#include <stdio.h>

int n; //initialization of number of lines
float a[1000][1100]; //array it self

int main(void) { //main
int i, j; //i = row
//j = column
FILE *fin = fopen("gaussian.txt", "r"); //opens the text file
fscanf(fin, "%d", &n); //scans for number of lines and stores in n
for (i = 0; i < n; ++i) //for loop (row)
for (j = 0; j < n + 1; ++j){ //for loop (column)
fscanf(fin, "%f", &a[i][j]); //scans into the array
}
fclose(fin); //closes the file
return 0; //return value
}

gaussian.txt contains:

2
-2 4 4
5 8 3

Have you verified that fopen() actually gives you a FILE*, and not NULL? If it cannot open the file, then the fscanf() function will cause a segfault. This is a good example why you REALLY want to test the results of functions such as fopen(), to verify that they were successful.

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.