输出传输到新文件

发布于 2024-09-15 20:07:20 字数 1756 浏览 9 评论 0原文

我对c完全是菜鸟。我从互联网上下载了一个代码。它生成所有可能的字符组合。我想将此输出传输到文本文件。我尝试了一些事情但无法做到。有人可以建议我进行必要的更改吗?这是代码。a

编辑:我已将 put 命令更改为 fputs。我尝试了所有可能的组合。fputs(&str[1],f), fputs("&str[1]" ,"f"), fputs("&str[1]",f) 和 fputs( &str,“f”)。但没有任何作用。接下来该怎么办?

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

#define MINCHAR 33
#define MAXCHAR 126

char *bruteforce(int passlen, int *ntries);

int main(void) {

    int i, wdlen, counter;
    char *str;
    clock_t start, end;
    double elapsed;
    FILE *myfile;
    myfile = fopen("ranjit.txt","w");

    do {
        printf("Word length... ");
        scanf("%d", &wdlen);
    } while(wdlen<2);

    start = clock();

    bruteforce(wdlen, &counter);

    end = clock();

    elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
    fprintf(myfile,"\nNum of tries... %d \n",counter);
    fprintf(myfile,"\nTime elapsed... %f seconds\n",elapsed);
    return counter;

}

char *bruteforce(int passlen, int *ntries) {

    int i;
    char *str;
    FILE *f;
    f = fopen("sandip.txt","w");

    *ntries=0;

    passlen++;

    str = (char*)malloc( passlen*sizeof(char) );

    for(i=0; i<passlen; i++) {
        str[i]=MINCHAR;
    }
    str[passlen]='\0';

    while(str[0]<MINCHAR+1) {
        for(i=MINCHAR; i<=MAXCHAR; i++) {
            str[passlen-1]=i;
            (*ntries)++;
            fputs("&str[1]",f);
        }

        if(str[passlen-1]>=MAXCHAR) {
            str[passlen-1]=MINCHAR;
            str[passlen-1-1]++;
        }

        for(i=passlen-1-1; i>=0; i--) {
            if(str[i]>MAXCHAR) {
                str[i]=MINCHAR;
                str[i-1]++;
            }
        }
    }

    return NULL;

}

I am a complete noob to c. I have downloaded a code from internet. It generates all possible combination of characters. I want to transfer this output to a text file. I have tried few things but unable to do it. Can anybody suggest me the necessary changes? Here is the code.a

EDIT: I have changed the put command to fputs. i tried all possible combinations.fputs(&str[1],f), fputs("&str[1]" ,"f"), fputs("&str[1]",f) and fputs(&str,"f"). but nothing worked. what to do next?

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

#define MINCHAR 33
#define MAXCHAR 126

char *bruteforce(int passlen, int *ntries);

int main(void) {

    int i, wdlen, counter;
    char *str;
    clock_t start, end;
    double elapsed;
    FILE *myfile;
    myfile = fopen("ranjit.txt","w");

    do {
        printf("Word length... ");
        scanf("%d", &wdlen);
    } while(wdlen<2);

    start = clock();

    bruteforce(wdlen, &counter);

    end = clock();

    elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
    fprintf(myfile,"\nNum of tries... %d \n",counter);
    fprintf(myfile,"\nTime elapsed... %f seconds\n",elapsed);
    return counter;

}

char *bruteforce(int passlen, int *ntries) {

    int i;
    char *str;
    FILE *f;
    f = fopen("sandip.txt","w");

    *ntries=0;

    passlen++;

    str = (char*)malloc( passlen*sizeof(char) );

    for(i=0; i<passlen; i++) {
        str[i]=MINCHAR;
    }
    str[passlen]='\0';

    while(str[0]<MINCHAR+1) {
        for(i=MINCHAR; i<=MAXCHAR; i++) {
            str[passlen-1]=i;
            (*ntries)++;
            fputs("&str[1]",f);
        }

        if(str[passlen-1]>=MAXCHAR) {
            str[passlen-1]=MINCHAR;
            str[passlen-1-1]++;
        }

        for(i=passlen-1-1; i>=0; i--) {
            if(str[i]>MAXCHAR) {
                str[i]=MINCHAR;
                str[i-1]++;
            }
        }
    }

    return NULL;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流云如水 2024-09-22 20:07:20

puts 替换为 fputs,它采用 FILE* 作为第二个参数。您需要将其传递给bruteforce

Replace puts with fputs, which takes a FILE* as its second argument. You will need to pass that in to bruteforce.

伏妖词 2024-09-22 20:07:20

对于它的价值,您可以使用 shell 重定向来创建一个包含输出的文件...

./myprogram > output.txt

For what it's worth, you can use shell redirection to create a file with the output...

./myprogram > output.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文