tags:

views:

45

answers:

2

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;

}
+2  A: 

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

mathepic
thanks mathepic. I will google the syntax of fputs.
@user427685: It would be good for mathepic to accept the answer.
FUZxxl
@FUZxxl I can't accept the answer.
mathepic
question edited guys. I tried fputs as suggested by mathepic. nothing worked. Expecting more suggestions.
Can you at least TELL us what didn't work?
mathepic
I just tried implementing my suggestion on your code and it works fine.
mathepic
@mathepic can u provide me the syntax you tried.When I tried fputs, it stopped to generate output. or the exe stops to work in between.
It takes time to write large amounts of output. I declared `static FILE* myfile;` at the top instead of in `main`, and used `fputs(`
mathepic
@mathepic thank you very much. It worked. You helped me a lot.thanks again.
Okay, can you accept the answer? And I still don't understand why you are using the harddrive.
mathepic
how to accept the answer? this is my first ques on this site. I don't know how to do it?
There should be a big checkmark you can click.
mathepic
A: 

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

./myprogram > output.txt
gahooa
Output of this program will work as input for other, so i can't use this method.
mathepic
I did not explained it earlier properly. It's like two functions of a program. output of a function will work as input for other. Can't use command line.
So why are we writing to the harddrive between the functions?
mathepic