i am trying to save a matrix of doubles to a file with name Output.txt in C programming.
My code until now is and i have the questions as comments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// matrix[i][j] contain doubles and its size is [100][100], so N=100.
FILE *out_file;
out_file = fopen("Output.txt", "w");
if (out_file == NULL) {
fprintf(stderr,"Can not open output file\n");
exit (8);
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
fputc(matrix[i][j],out_file);// what function should i use here ? How to cast from character to double ??
fclose(out_file);
Well, you're already using fprintf for your errors. You could use that for the output, too. Rather than converting and then outputting as separate steps.
1 2 3
for(i=0;i<N;i++)
for(j=0;j<N;j++)
fprintf(out_file, "%f\n", matrix[i][j]); // assuming one value per line here