簡體   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;
 }

嗨,我正在嘗試混淆解算器。我想將置換的結果存儲到緩沖區,然后從緩沖區存儲到某個文件,以便我可以將它與字典進行比較。 得到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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM