简体   繁体   English

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

[英]how to store o/p into a buffer,and then from buffer to file

#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;
 }

Hi ,I am trying jumble solver .I want to store the result of permutation to a buffer and then from buffer to some file,so that I can compare it with dictionary. 嗨,我正在尝试混淆解算器。我想将置换的结果存储到缓冲区,然后从缓冲区存储到某个文件,以便我可以将它与字典进行比较。 getting errors for setvbuf. 得到setvbuf的错误。 My C is very rusty,not able to get the desired results. 我的C非常生疏,无法获得理想的效果。

there are plenty of errors. 有很多错误。 First you use setvbuf with wrong arguments and do that in cycle. 首先使用带有错误参数的setvbuf并在循环中执行此操作。 Then you fprintf buffer value instead of str. 然后你fprintf缓冲值而不是str。 And also close file in cycle. 并且还在循环中关闭文件。

#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 is used to set custom buffer. setvbuf用于设置自定义缓冲区。 It is used, for example, to set big buffer and to have direct access to it. 例如,它用于设置大缓冲区并直接访问它。 fprintf will print to file only when buffer is full, or when you flush, or close file. fprintf仅在缓冲区已满或刷新或关闭文件时才会打印到文件。 And you also use _IONBF which means that stream is not buffered at all. 而且你也使用_IONBF,这意味着流根本没有缓冲。

So - it works only, because it is not buffered, as you send buffer [100], and then try to print 1024. So also change size of buffer to 1024 and use _IOFBF. 所以 - 它只是工作,因为它没有缓冲,因为你发送缓冲区[100],然后尝试打印1024.所以也将缓冲区的大小更改为1024并使用_IOFBF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM