简体   繁体   中英

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. My C is very rusty,not able to get the desired results.

there are plenty of errors. First you use setvbuf with wrong arguments and do that in cycle. Then you fprintf buffer value instead of 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. 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. And you also use _IONBF which means that stream is not buffered at all.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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