我有用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

09-09 20:36
查看更多