質問

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 < size; i++) {
     printf("%d", (int)v[i]);
     fprintf(fpIn, "%d\n", (int)v[i]);   
   }
   perm_count++;
   printf("\n");
 }
 fclose(fpIn);
} 

I guess this is a relatively simple question :)

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 !

I will paste the code below, and help is much appreciated ! The write to file code is found within the print function.

Thanks,

T.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>

#include <time.h>
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 < 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 < 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", &x);
 int arr[x];   
 printf("Please enter the terms: ");
 for(i = 0; i < x; i++)
 scanf("%d", &arr[i]);
 START
 permute(arr, 0, sizeof(arr)/sizeof(int));
 STOP   
 printf("Permutation Count: %d\n", perm_count);
 PRINTTIME
 return 0;
}
役に立ちましたか?

解決

1. Incorrect access modes in fopen call
You open your file as a binary file: fopen("char-array.txt", "wb");. Don't put b to this string containing access modes if you are going to write formatted strings there. And since you probably want to append new data at the end of the file instead of overwritting them, use a instead of w:

fopen("char-array.txt", "a");

2. Writing to the output buffer, not directly into the file
When you are using functions like fprintf, you don't write directly to the file but to the output buffer. You have to use fflush to write data from the output buffer into the file, or you can just close your file by using fclose function which flushes this buffer automatically.

Just add this line:

fclose(fpIn);

at the end of print function.

3. Incorrect formatting of the output
You should not cast int to char. It will truncate your numbers. And you also have fprintf(fpIn, "\n"); in wrong scope I guess. It could look like this:

for (i = 0; i < size; i++) {
  printf("%d ", v[i]);
  fprintf(fpIn, "%d ", v[i]);
}
perm_count++;
printf("\n");
fprintf(fpIn, "\n");

他のヒント

Don't waste your time doing programming you don't have to, the use of fprintf is nice but since all you want to do is print the output, you can just print things into the file directly using UNIX built-in commands. Say your program is called wirteoutput then all you have to do is pass the following command when calling it from the shell writeoutput > file.txt. All you would have to use would be the printf function.

If you are curious about this, this is an old function and you can find a detailed description in the original paper The UNIX Operating System. Look at the section called Standard I/O.

You didn't cast to a char (from a int) when you wrote to the file as you did with the screen display. The following will provide the same numbers in the file as you're seeing on screen:

fprintf(fpIn, "%d", (char)v[i]);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top