我想追加一个字符串到文本文件,但我变得奇怪的字符。 如果我尝试将我的结果打印到控制台,则输出是常规的。 这就是我在vim中看到的输出。如果我在gedit中查看它,我会得到奇怪的盒子。奇怪的输出写入文件在C
输出文件:
A^CA^BB^A
A^CB^BA^A
B^CA^BA^A
预期输出:
AAB
ABA
BAA
我的功能应该没有重复的排列写出到一个文本文件中。
代码对于功能:
void RecursivePermute (char *prefix, char *rest, int *ptr)
{
char *temp = malloc(sizeof(char *));
char *new_prefix = malloc(sizeof(char *));
char *rest_left = malloc(sizeof(char *));
char *rest_right = malloc(sizeof(char *));
char *new_rest = malloc(sizeof(char *));
char rest_char;
int idx = 0;
int first_occurance = 0;
int i;
FILE *file;
strcpy(temp, rest);
if (*rest == '\0')
{
*ptr += 1;
printf("Permutation %d: %s\n", *ptr, prefix);
file = fopen("permutations.txt", "a");
fprintf(file,"%s\n",prefix);
fclose(file);
return;
}
else
{
size_t rest_size = strlen(rest);
while (*rest != '\0')
{
first_occurance = (strchr(temp, *rest) - temp - idx);
if (first_occurance == 0)
{
rest_char = *rest;
rest_left = strncpy(rest_left, rest-idx, idx);
rest_right = strncpy(rest_right, rest+1, rest_size-1);
sprintf(new_rest, "%s%s", rest_left, rest_right);
sprintf(new_prefix,"%s%s", prefix, &rest_char);
RecursivePermute(new_prefix, new_rest, ptr);
}
rest++;
idx ++;
}
}
}
如果您还显示对该函数的调用,显示您传递的参数并显示结果将会很有帮助。 – steveha
@steveha我正在用RecursivePermute(“”,letters,&count)调用函数; //其中count = 0;和字母[] =“AAB”; – Miles