如何将o/p存储到一个缓冲区,然后从一个缓冲区存储到另一个文件 #包括 #包括 #包括 #包括 int比较(常数void*a,常数void*b){ return(*(char*)a-*(char*)b);} //一个实用函数,用于交换两个字符A和b 无效交换(字符*a,字符*b) { chart=*a; *a=*b; *b=t;} int findCeil(char str[],char first,int l,int h) { //初始化天花板元素的索引 int ceilIndex=l; int i; //现在遍历其余元素并找到 //大于“first”的最小字符 对于(i=l+1;i首先&&str[i]=0;--i) if(str[i]
您好,我正在尝试混合解算器。我想将排列结果存储到缓冲区,然后从缓冲区存储到某个文件,以便与字典进行比较。获取setvbuf的错误。如何将o/p存储到一个缓冲区,然后从一个缓冲区存储到另一个文件 #包括 #包括 #包括 #包括 int比较(常数void*a,常数void*b){ return(*(char*)a-*(char*)b);} //一个实用函数,用于交换两个字符A和b 无效交换(字符*a,字符*b) { chart=*a; *a=*b; *b=t;} int findCeil(char str[],char first,int l,int h) { //初始化天花板元素的索引 int ceilIndex=l; int i; //现在遍历其余元素并找到 //大于“first”的最小字符 对于(i=l+1;i首先&&str[i]=0;--i) if(str[i],c,file,C,File,您好,我正在尝试混合解算器。我想将排列结果存储到缓冲区,然后从缓冲区存储到某个文件,以便与字典进行比较。获取setvbuf的错误。 我的C非常生疏,无法获得所需的结果。有很多错误。首先,将setvbuf与错误的参数一起使用,并循环使用。然后,您可以使用fprintf缓冲区值而不是str。并在循环中关闭文件 #include <stdio.h> #include <stdlib.h> #include <string.h> #include<stdbool
我的C非常生疏,无法获得所需的结果。有很多错误。首先,将setvbuf与错误的参数一起使用,并循环使用。然后,您可以使用fprintf缓冲区值而不是str。并在循环中关闭文件
#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;
}
#包括
#包括
#包括
int比较(常数void*a,常数void*b){
返回(*(字符*)a-*(字符*)b);
}
无效交换(字符*a,字符*b){
chart=*a;
*a=*b;
*b=t;
}
int findCeil(常量字符str[],字符优先,int l,int h){
int ceilIndex=l;
int i;
对于(i=l+1;i首先&&str[i]=0;--i)
if(str[i]
setvbuf用于设置自定义缓冲区。例如,它用于设置大缓冲区并直接访问它。fprintf将仅在缓冲区已满、刷新或关闭文件时打印到文件。您还使用了_IONBF,这意味着流根本没有缓冲
所以-它只起作用,因为它没有缓冲,因为您发送缓冲区[100],然后尝试打印1024。所以也可以将缓冲区的大小更改为1024,并使用_IOFBF
#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;
}