我是c的完整菜鸟。我从互联网上下载了一个代码。它会生成所有可能的字符组合。我想将此输出传输到文本文件。我尝试了一些但却无法做到的事情。任何人都可以建议我做出必要的改变吗?这是code.a
编辑:我已将put命令更改为fputs。我尝试了所有可能的combination.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;
}
答案 0 :(得分:2)
将puts
替换为fputs
,其中FILE*
为第二个参数。您需要将其传递给bruteforce
。
答案 1 :(得分:0)
对于它的价值,你可以使用shell重定向来创建一个带有输出的文件......
./myprogram > output.txt