将c代码执行的结果写入txt

将c代码执行的结果写入txt,c,printf,C,Printf,我得到了一个生成所有可能的项链的代码,这个项链的密度是固定的,这是我工作所需要的。问题是我太笨了,无法解决以下问题:如何将此代码的结果打印到txt而不是控制台上? 当输出足够大时,我看不到所有的项链,显然我需要所有的项链。对不起,我写得很糟糕 /********************************************************************* * C program to generate fixed density necklaces and Lyndo

我得到了一个生成所有可能的项链的代码,这个项链的密度是固定的,这是我工作所需要的。问题是我太笨了,无法解决以下问题:如何将此代码的结果打印到txt而不是控制台上? 当输出足够大时,我看不到所有的项链,显然我需要所有的项链。对不起,我写得很糟糕

/*********************************************************************
* C program to generate fixed density necklaces and Lyndon words.    *
* The algorithm is CAT and is described in the paper by Sawada and   *
* Ruskey "An efficient algorithm for generating necklaces with fixed *
* density."   This program, was obtained from the                           *
* (Combinatorial) Object Server, COS, at http://www.theory.csc.uvic.ca *
* The inputs are n, the length of the string, k, the arity of the           *
* string, and d the density, the number of non-0's in the string.    *
* The program can be modified, translated to other languages, etc.,  *
* so long as proper acknowledgement is given (author and source).    *
* Programmer: Joe Sawada                                             *
**********************************************************************/

/*---------------------------------------------------*/
/* This program generates all k-ary fixed density    */ 
/*      necklaces, Lyndon words or prencklaces       */
/*      with length n and density d in lex order     */ 
/*---------------------------------------------------*/

#include <stdio.h>
#include <math.h>

int a[100];
int b[100];
int n,k,d,total;
char type;

void Print(int p) {

    int i,j,next,end,min;

    /* Determine minimum position for next bit */
    next =  (d/p)*a[p] + a[d%p];    
    if (next < n) return;

    /* Determine last bit */
    min = 1;    
    if (type == 'p') min = k-1;
    else if ((next == n) && (d%p != 0)) {
        min =  b[d%p]+1;
        p = d;
    }
    else if ((next == n) && (d%p == 0)) {
        min = b[p];
    }

    /* Determine length of String */
    end = n;
    if (type == 'p') end = n-1; 

    for( b[d]=min; b[d]<k; b[d]++ ) {   
        i = 1;
        /* Test for lyndon words */
        if ( (type == 'l') && (n%a[p] == 0) && (a[p] != n)) {}
        else {  
            for(j=1; j<=end; j++) {
                if (a[i] == j) {
                    printf("%d ",b[i]);
                    i++;
                }
                else printf("0 ");  
            }
            printf("\n"); 
            total++;
        }
        p = d;
    }     
}

void Gen(int t,int p) {

    int i,j,max,tail;

    if (t >= d-1) Print(p);
    else {
        tail = n - (d - t) + 1;
        max = ((t+1)/p)*a[p] + a[(t+1)%p];
        if (max <=tail) {
            a[t+1] = max;
            if ((t+1)%p == 0) b[t+1] = b[p];
            else b[t+1] = b[(t+1)%p];

            Gen(t+1,p);
            for (i=b[t+1] +1; i<k; i++) {
                b[t+1] = i;
                Gen(t+1,t+1);
            }
            tail = max-1;
        }
        for(j=tail; j>=a[t]+1; j--) {
            a[t+1] =  j;
            for (i=1; i<k; i++) {
                b[t+1] =  i;
                Gen(t+1,t+1);
            }
        } 
    }
} 

void Fixed() {

    int i,j;

    /* initialize string */
    for(j=0; j<=d; j++) a[j] = 0;

    if (d == 0) {
        if (type == 'n') {
            for (j=1; j<=n; j++) printf("0 ");
            printf("\n");
            total = 1; 
        }
    } 
    else if (d == 1) {
        for (i=1; i<k; i++) {
            for (j=1; j<n; j++) printf("0 ");
            printf("%d \n",i);
        }
        total = k-1; 
    } 
    else {
        /* For prenecklaces we generate necklaces with */
                /* density n+1 and density d+1 and then ignore */ 
                /* the last bit                                */       
        if (type == 'p') { n++; d++; } 

        a[0] = 0;
        a[d] = n;   
        for(j=n-d+1; j>=(n-1)/d + 1; j--) {
            a[1] = j;
            for (i=1; i<k; i++) {
                b[1] = i;
                Gen(1,1);       
            }
        } 
    }
}

void main() {

    int i,j;

    /* type: 'n' necklace or 'l' Lyndon word or 'p' prenecklace */
    printf("Enter n k d type: ");
    scanf("%d %d %d %c", &n, &k, &d, &type);  

    total = 0;
    Fixed();        
    printf("Total = %d\n",total);  
}
/*********************************************************************
*C程序生成固定密度的项链和林登字*
*该算法为CAT算法,文中由Sawada和*
*Ruskey“一种生成固定长度项链的有效算法*
*该程序从*
*(组合)对象服务器,COS,athttp://www.theory.csc.uvic.ca *
*输入是n,字符串的长度,k,字符串的算术数*
*字符串,d表示密度,即字符串中非0的数量*
*该程序可以修改、翻译成其他语言等*
*只要给予适当的承认(作者和来源)*
*程序员:Joe Sawada*
**********************************************************************/
/*---------------------------------------------------*/
/*此程序生成所有k元固定密度*/
/*项链、林登语或Prenckles*/
/*长度为n,密度为d,按lex顺序*/
/*---------------------------------------------------*/
#包括
#包括
INTA[100];
int b[100];
整数n,k,d,总计;
煤焦类型;
作废打印(INTP){
int i,j,next,end,min;
/*确定下一位的最小位置*/
下一步=(付款交单)*a[p]+a[d%p];
如果(下一个对于(b[d]=min;b[d]有两种方法:

  • 如果您在Linux上,当您想要执行您的程序时,您只需
    /myprogram 2>&1 | tee output.txt
    ,这允许您将输出写入控制台和output.txt文件
  • 您可以使用系统调用将标准输出文件描述符从1(请参阅:)更改为要将输出写入的文件的文件描述符。要获取要写入输出的文件的文件描述符,请使用系统调用open(请参阅:),如您所见,它将返回文件描述符。最后,您只需使用系统调用dup2(请参阅:)将stdout文件描述符与文件描述符交换。不要忘记在结束时使用系统调用close关闭文件(请参阅:)
  • 注意:由于您在程序开始时仍然需要一些用户交互

    printf("Enter n k d type: ");
    scanf("%d %d %d %c", &n, &k, &d, &type); 
    

    如果您选择第二个选项,您可以在交互之后交换文件描述符,最后当您想要显示结果时,只需再次交换。

    保持第一个printf和scanf不变-这些是程序的参数(您可以从文件中获取它们,但我假设通过说“代码的结果”你指的是算法的结果)

    考虑到这一点,我们需要关注固定、生成和打印功能:

    首先-您使用的这一数量的全局变量被认为是不好的做法:

    • 更难分析函数的代码
    • 这些变量的名称告诉我们什么——n,k,d,a,b的用途是什么
    当您处理这个问题时,为了写入文件,您将需要向每个函数传递另一个参数,您希望重定向到该文件的输出

    因此,
    fopen()
    main中需要的文件(您可以提示用户输入要保存的文件名):


    在离开
    main()之前,将文件传递到
    Fixed()
    Print()
    ,将
    printf(…)
    更改为
    fprintf(文件…)
    fclose()

    解释不成功。欢迎使用堆栈溢出。您的问题有一个问题:您告诉我们的程序做了什么太多,没有做什么太少。我们不需要知道项链或任何其他装饰:),我们也不需要您程序的全部源代码…这会分散您实际问题的注意力。这似乎是关于将输出写入文件而不是控制台。请尝试将您的解释和代码示例仅限于与核心问题相关的内容。将程序张贴在您使用过的
    fprintf
    。还提到了失败的原因,即它没有创建任何文件或创建空文件或其他任何文件。我的问题是,当生成的项链数量很大时,即使向上滚动,我也无法在控制台输出上看到所有结果,因此,我需要将结果写入文件,或者可能是另一种解决方案不知道。我已经编写了所有代码,以防有人需要回答我的问题。学习文件处理并将输出写入txt文件。我相信参数应该来自控制台,但结果本身应该放在文件中,所以为什么不直接使用
    fprintf
    file = fopen(file_name, "w");
    if (!file) {
       /* handle file opening error */
       ...
    }