I'm having trouble writing an array of double numbers in a text file. The problem is, the code runs, but doesn't write out anything.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *out;
double numbers[30];
int i=0;
for(i;i<30;i++)
scanf("%lf", &numbers[i]);
out=fopen("out.txt", "w");
if (out == NULL) {
fprintf(stderr, "error in opening .txt");
exit(EXIT_FAILURE);
}
while ( i<30 ) {
fprintf(out, "%.3f", numbers[i]);
i++;
}
fclose(out);
return 0;
}
Basically, the code is supposed to write out an array of 30 double numbers in a text file, and round the decimals to '.3'.
i. Use braces aroundscanfand check the return value