开发者

output transfer to a new file

开发者 https://www.devze.com 2023-01-12 09:37 出处:网络
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 unabl

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;

}


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


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

./myprogram > output.txt
0

精彩评论

暂无评论...
验证码 换一张
取 消