2015-02-10 50 views
-2

我得到了一個代碼,用於生成固定密度的所有可能的項鍊,這是我需要的一項工作。問題是,我太noob在C來解決以下問題:我如何可以將此代碼的結果打印到txt而不是控制檯? 當輸出足夠大我看不到所有的項鍊,顯然我需要所有。對不起,我可怕的寫作。在txt中寫入c代碼執行的結果

/********************************************************************* 
* 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); 
} 

我試圖使用fprintf(....);代替printf(...);指向到一個文件中沒有成功

+1

解釋*沒有成功*。 – 2015-02-10 10:45:39

+3

歡迎來到Stack Overflow。你的問題有一個問題:你告訴我們你的程序做了太多事情,並且太少了解它不做的事情。我們不需要了解項鍊或任何其他裝飾品:),我們也不需要您的程序的全部源代碼...這是相當分心的實際問題。這似乎是將輸出寫入文件而不是控制檯。儘量將你的解釋和代碼範例限制在與核心問題相關的部分。 – stakx 2015-02-10 10:46:27

+0

發佈已使用'fprintf'的程序。並且還提到了什麼是失敗,即_it沒有創建任何file_或_created空文件_或_whatever它是__。 – 2015-02-10 10:56:34

回答

0

有兩種方法可以做到這一點:

  1. 如果你是在Linux上你可以./myprogram 2>&1 | tee output.txt時你想執行你的程序,這允許你編寫輸出來控制文件output.txt。
  2. 可以使用系統調用將標準輸出文件描述符從1(請參閱:http://en.wikipedia.org/wiki/File_descriptor)更改爲要將輸出寫入到的文件的文件描述符。要獲得要寫入的文件的文件描述符輸出,請使用系統調用open(請參閱:http://codewiki.wikidot.com/c:system-calls:open),您可以看到它將返回文件描述符。最後,您只需使用系統調用dup2將stdout文件描述符與文件文件描述符交換(請參閱:http://linux.die.net/man/2/dup2)。不要忘記關閉該文件在使用系統調用接近結束(見:http://codewiki.wikidot.com/c:system-calls:close

注:因爲你仍然需要在你的程序

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

的開始,如果你選擇一些用戶交互第二個選項,你可以在交互後交換文件描述符,最後當你想顯示結果時,只需再次交換。

+0

我相信參數應該來自控制檯,但結果本身應該放在文件中,爲什麼不使用'fprintf'? – zoska 2015-02-10 11:59:50

0

離開第一個printf和scanf,因爲它是 - 這是程序的參數(你可以從文件中獲取他們,但我認爲說「的代碼,結果」你的意思是算法的結果)

隨着考慮到這一點,我們需要把重點放在固定的,根和打印功能:

首先 - 這一數額使用的是被認爲是不好的做法全局變量:

  • 它很難分析的代碼功能
  • 這些變量的名稱告訴我們什麼 - n,k,d,a,b的目的是什麼?

當您處理此操作時,對於寫入文件,您需要將另一個參數傳遞給每個函數,您要將哪個輸出重定向到文件。

所以在主fopen()所需的文件(可以爲要保存的文件名提示用戶):

file = fopen(file_name, "w"); 
if (!file) { 
    /* handle file opening error */ 
    ... 
} 

通行證文件Fixed()Print(),留下main()之前更改printf(...)fprintf(file, ...)fclose()