#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<stdbool.h>
int compare (const void *a, const void * b){
return ( *(char *)a - *(char *)b ); }
// A utility function two swap two characters a and b
void swap (char* a, char* b)
{
char t = *a;
*a = *b;
*b = t; }
int findCeil (char str[], char first, int l, int h)
{
// initialize index of ceiling element
int ceilIndex = l;
int i;
// Now iterate through rest of the elements and find
// the smallest character greater than 'first'
for (i = l+1; i <= h; i++)
if (str[i] > first && str[i] < str[ceilIndex])
ceilIndex = i;
return ceilIndex;
}
// Print all permutations of str in sorted order
void sortedPermutations ( char str[] )
{
FILE *fp;
fp = fopen("out.txt","w+");
char buffer[100];
memset(buffer,'\0',100);
// Get size of string
int size = strlen(str);
// Sort the string in increasing order
qsort( str, size, sizeof( str[0] ), compare );
// Print permutations one by one
bool isFinished = false;
while ( ! isFinished )
{
// print this permutation
setvbuf(str, buffer, _IONBF, 1024);
printf ("%s \n", str);
fprintf(fp,"%s\n",buffer);
// Find the rightmost character which is smaller than its next
// character. Let us call it 'first char'
int i;
for ( i = size - 2; i >= 0; --i )
if (str[i] < str[i+1])
break;
// If there is no such chracter, all are sorted in decreasing order,
// means we just printed the last permutation and we are done.
if ( i == -1 )
isFinished = true;
else
{
// Find the ceil of 'first char' in right of first character.
// Ceil of a character is the smallest character greater than it
int ceilIndex = findCeil( str, str[i], i + 1, size - 1 );
// Swap first and second characters
swap( &str[i], &str[ceilIndex] );
// Sort the string on right of 'first char'
qsort( str + i + 1, size - i - 1, sizeof(str[0]), compare );
}
fclose(fp);
}
}
int main()
{
char str[] = "ABCD";
sortedPermutations( str );
return 0;
}
嗨,我正在尝试混淆解算器。我想将置换结果存储到缓冲区,然后从缓冲区存储到某个文件,以便我可以将它与字典进行比较。得到setvbuf的错误。 我的C非常生疏,无法获得理想的结果。
答案 0 :(得分:0)
有很多错误。首先使用带有错误参数的setvbuf并在循环中执行此操作。然后你fprintf缓冲值而不是str。并且还在循环中关闭文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare (const void *a, const void * b) {
return ( *(char *)a - *(char *)b );
}
void swap (char* a, char* b) {
char t = *a;
*a = *b;
*b = t;
}
int findCeil (const char str[], char first, int l, int h) {
int ceilIndex = l;
int i;
for (i = l+1; i <= h; i++) {
if (str[i] > first && str[i] < str[ceilIndex]) {
ceilIndex = i;
}
}
return ceilIndex;
}
void sortedPermutations ( char str[] ) {
FILE *fp;
char buffer[1024];
int size = strlen(str);
int isFinished = 0;
fp = fopen("out.txt","w+");
memset(buffer, 0, 100);
qsort( str, size, sizeof( str[0] ), compare );
setvbuf(fp, buffer, _IOFBF, 1024);
while ( !isFinished ) {
int i;
printf("%s \n", str);
fprintf(fp, "%s\n", str);
for ( i = size - 2; i >= 0; --i )
if (str[i] < str[i+1]) {
break;
}
if ( i == -1 ) {
isFinished = 1;
} else {
int ceilIndex = findCeil( str, str[i], i + 1, size - 1 );
swap( &str[i], &str[ceilIndex] );
qsort( str + i + 1, size - i - 1, sizeof(str[0]), compare );
}
}
fclose(fp);
}
int main() {
char str[] = "ABCD";
sortedPermutations( str );
return 0;
}
setvbuf用于设置自定义缓冲区。例如,它用于设置大缓冲区并直接访问它。 fprintf仅在缓冲区已满或刷新或关闭文件时才会打印到文件。而且你也使用_IONBF,这意味着流根本没有缓冲。
所以 - 它只是工作,因为它没有缓冲,因为你发送缓冲区[100],然后尝试打印1024.所以也将缓冲区的大小改为1024并使用_IOFBF。