gpt4 book ai didi

c - 如何将o/p存储到缓冲区中,然后从缓冲区存储到文件

转载 作者:行者123 更新时间:2023-11-30 17:20:22 25 4
gpt4 key购买 nike

#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很生疏,达不到想要的结果。

最佳答案

有很多错误。首先,您使用带有错误参数的 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。

关于c - 如何将o/p存储到缓冲区中,然后从缓冲区存储到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com