输出传输到新文件
我对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
puts
替换为fputs
,它采用FILE*
作为第二个参数。您需要将其传递给bruteforce
。Replace
puts
withfputs
, which takes aFILE*
as its second argument. You will need to pass that in tobruteforce
.对于它的价值,您可以使用 shell 重定向来创建一个包含输出的文件...
For what it's worth, you can use shell redirection to create a file with the output...