簡體   English   中英

如何將控制台輸出寫入文本文件

[英]How to write console output to a text file

我有這個用C語言生成的密鑰生成算法,它將在控制台中顯示所有生成的密鑰:

那么如何將寫入控制台的所有密鑰保存到文本文件中呢?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

static const char alphabet[] = "abcdefghijklmnopqrs";               
static const int alphabet_size = sizeof(alphabet) - 1;

void brute_impl(char * str, int index, int max_depth)
{
    int i;
    for (i = 0; i < alphabet_size; ++i)
    {
        str[index] = alphabet[i];

        if (index == max_depth - 1)
        {   
            printf("%s\n", str); // put check() here instead                     
        }
        else
        {
            brute_impl(str, index + 1, max_depth);
        }
    }
}

void brute_sequential(int max_len)
{
    char * buf = malloc(max_len + 1);
    int i;

    for (i = 8; i <= max_len; ++i)
    {
    memset(buf, 0, max_len + 1);
    brute_impl(buf, 0, i);
    }
    free(buf);
}

int main(void)
{
    brute_sequential(8);
    return 0;
}

專業方式:

在代碼的開頭使用以下代碼

freopen("output.txt","w",stdout);

另一種直觀方式:

在Windows中,一旦編譯了C代碼,它就會生成一個.exe文件(比如說是dummy.exe)。

現在將這個exe文件生成的輸出保存到外部txt文件(比如它是out.txt),你需要做的就是通過CMD瀏覽到那個exe文件並輸入

dummy.exe > out.txt

如果您有任何輸入要檢查,請使用

dummy.exe <input.txt> out.txt

您可以使用重定向窗口 )和( * nix )將控制台輸出從任何語言的任何程序發送到文本文件:

# to overwrite an existing file:
./my_program.a > my_outfile.txt

# or to append
./my_program.a >> my_outfile.txt

暫無
暫無

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

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