wierd segmentation fault with fopen
So, I use the following code with a pre-existing txt file. It works perfectly fine on my MAc laptop and segmentation faults on my linux machine. Any suggestions would be appreciated. I basically need to create a file in IDL then read it into C so any methodology will work.
Thanks,
Jared
Code:
#include <stdio.h>
#include <stdlib.h>
FILE *in_file, *out_file;
/*out_file should be an axact duplicate of in_file (the file created in IDL)*/
/*program to open a text file from idl and read it into c*/
int main()
{
char temp[80]; /*temporary string to store line from file in */
int values; /*value of line, will be velocity or density */
int i,j,ii,jj; /*keep grid point data and use as a check later*/
int max_i=511, max_j=511; /*control variables for file i/o, set by nput grid size*/
int done; /*used to close file*/
int spectrum[max_i][max_j]; /*stores data*/
/*open idl file*/
in_file=fopen("cloud.txt","r");
if(in_file == NULL)
{
printf("Failed to open in file!\n");
exit(-1);
}
/*open duplicate file*/
out_file=fopen("c.txt", "w");
if(out_file == NULL)
{
printf("Failed to open out file!\n");
exit(-1);
}
/*Now read from infile and write to out file*/
for (ii = 0; ii<max_i+1; ++ii)
{
for (jj = 0; jj<max_j+1; ++jj)
{
fgets(temp, sizeof(temp), in_file);
sscanf(temp, "%d", &values);
fputs(temp, out_file);
spectrum[ii][jj]=values;
/* printf("%d %d\n",ii,jj);*/
}
}
done = close(out_file);
done = close(in_file);
return(0);
}