title: C - writing output to a file
body: <pre><code>EDIT: 
  
  void print(const int *v, const int size) {
   FILE *fpIn;
   fpIn = fopen("char-array.txt", "a");
   int i;  
   if (v != 0) {
     for (i = 0; i &lt; size; i++) {
       printf("%d", (int)v[i]);
       fprintf(fpIn, "%d\n", (int)v[i]);   
     }
     perm_count++;
     printf("\n");
   }
   fclose(fpIn);
  } 
  </code></pre>
  
  <p>I guess this is a relatively simple question :) </p>
  
  <p>Basically the program is using a permutation algorithm, and printing the output to standard output in the console. I also want to write the content to a file via fprintf I assume. Though I cant seem to get it working. It just prints garbage characters into the first line in the text file and nothing more !</p>
  
  <p>I will paste the code below, and help is much appreciated ! The write to file code is found within the print function.</p>
  
  <p>Thanks,</p>
  
  <p>T.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  #include &lt;time.h&gt;
  clock_t startm, stopm;
  #define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
  #define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
  #define PRINTTIME printf("%2.3f seconds used by the processor.", ((double)stopm-          startm)/CLOCKS_PER_SEC);
  
  int perm_count = 0; 
  
  void print(const int *v, const int size) {
    FILE *fpIn;
    fpIn = fopen("char-array.txt", "wb");
    int i;  
    if (v != 0) {
      for (i = 0; i &lt; size; i++) {
        printf("%d", (char)v[i]);
        fprintf(fpIn, "%d", v[i]);  
        fprintf(fpIn, "\n");  
      }
      perm_count++;
      printf("\n");
    }
  } 
  
  
  void permute(int *v, const int start, const int n) {  
    int i;  
    if (start == n-1) {
      print(v, n);
    }
    else {
      for (i = start; i &lt; n; i++) {
        int tmp = v[i];
        v[i] = v[start];
        v[start] = tmp;
        permute(v, start+1, n);
        v[start] = v[i];
        v[i] = tmp;
      }
    }
  }
  
  int main() {
   int i, x;
   printf("Please enter the number of terms: ");
   scanf("%d", &amp;x);
   int arr[x];   
   printf("Please enter the terms: ");
   for(i = 0; i &lt; x; i++)
   scanf("%d", &amp;arr[i]);
   START
   permute(arr, 0, sizeof(arr)/sizeof(int));
   STOP   
   printf("Permutation Count: %d\n", perm_count);
   PRINTTIME
   return 0;
  }
  </code></pre>