c - Writing out an array using fprintf -


i'm having trouble writing array of double numbers in text file. problem is, code runs, 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, code supposed write out array of 30 double numbers in text file, , round decimals '.3'.

you forgot re-initialise i 0, hence current value of i 30, causes while loop not execute.

 = 0; //re-initialise i.  while ( i<30 ) {           fprintf(out, "%.3f", numbers[i]);          i++;      } 

it better, if use for loop, it's syntax helps remember initialise increment variable.

for (i = 0; < 30; ++i)      fprintf(out, "%.3f", numbers[i]); 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -