簡體   English   中英

C程序重定向到文件

[英]C program redirect to a file

我有以下代碼,如果我嘗試將其重定向到文件,則不會輸出任何提示。 它將提示和結果輸出都重定向到我的文件。

有沒有一種方法可以讓它輸出提示到stdout,然后輸出到文件?

/*prints chars in rows and columns*/
#include <stdio.h>

void display(char cr, int lines, int width); /*prototypes are fun*/

int main(void) {
    int ch; /*char to be printed*/
    int rows, cols; /*number of rows and columns*/
    printf("Enter a character and two integers.\n");

    while((ch = getchar()) != '\n') {
        if(scanf("%d %d", &rows, &cols) != 2) {
            break;
        }
        display(ch, rows, cols);
        while(getchar() != '\n') {
            continue;
        }
        printf("Enter another character and two integers. Newline quits.\n");
    }

    return 0;
}

void display(char cr, int lines, int width) {
    int row, col;
    for(row = 1; row <= lines; row++) {
        for(col = 1; col <= width; col++) {
            putchar(cr);
        }
        putchar('\n');
    }
}

您可能希望使文件名成為(可選)命令行參數。

為此,您可以將main的開頭更改為:

int main(int argc, char** argv) {
  FILE* outfile;
  if (argc == 2) outfile = fopen(argv[1], "w");
  else outfile = stdout;

(為安全起見,如果打開文件時發生錯誤,您可能應該檢查outfile是否不為null。您可能還希望檢查命令行參數的數量是否不正確,發出幫助消息等,具體取決於該程序適用於誰。)

然后,您必須將此FILE*傳遞給display函數,這需要更改該函數簽名,例如

void display(char cr, int lines, int width, FILE* outfile) {

最后,將putchar調用更改為putc以便將它們發送到文件,例如

putc('\n', outfile);

完成所有這些之后,像

./myprog

將打印所有提示以及輸出到stdout,而使用諸如

./myprog file.txt

仍將提示打印到標准輸出,但其他輸出將轉到file.txt

處理重定向輸出到文件時,可以使用freopen()

雖然這不是一個好的解決方案,但它是如何使用freopen()一個很好的例子。 假設您嘗試將stdout重定向到文件“ output.txt”,則可以編寫-

freopen("output.txt", "a+", stdout);  

這里的“ a +”表示追加模式。 如果文件存在,則文件以追加模式打開。 否則,將創建一個新文件。

freopen()重新打開stdout ,所有輸出語句( printfputchar )都重定向到'output.txt' 因此,當您嘗試重定向'printf("Enter another character and two integers. Newline quits.\\n");' 在終端/命令提示符中再次聲明,然后您必須使用以下代碼再次重新分配stdout

freopen("/dev/tty", "w", stdout); /*for gcc, ubuntu*/  

要么

freopen("CON", "w", stdout); /*Mingw C++; Windows*/  

這是完整的代碼-

/*prints chars in rows and columns*/
#include <stdio.h>

void display(char cr, int lines, int width); /*prototypes are fun*/

int main(void) {
    int ch; /*char to be printed*/
    int rows, cols; /*number of rows and columns*/

    printf("Enter a character and two integersk.\n");

    while((ch = getchar()) != '\n') {
        if(scanf("%d %d", &rows, &cols) != 2) {
            break;
        }

        freopen("output.txt", "a+", stdout);
        display(ch, rows, cols);
        freopen("/dev/tty", "w", stdout); /*for gcc, ubuntu*/
        //freopen("CON", "w", stdout); /*Mingw C++; Windows*/

        printf("Enter another character and two integers. Newline quits.\n");
        while(getchar() != '\n') {
            continue;
        }
        fclose(stdout);
    }

    return 0;
}

void display(char cr, int lines, int width) {
    int row, col;

    for(row = 1; row <= lines; row++) {
        for(col = 1; col <= width; col++) {
            putchar(cr);
        }
        putchar('\n');
    }
    return;
}

謝謝

暫無
暫無

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

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