我目前正在尝试读取文件并计算输入文件中用户指定字符串的实例数,并将它们输出到另一个文件中。
但是,当我尝试打开文件时,fopen
会返回NULL
。这是我到目前为止所做的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
//printf("string entered: %s\n%s\n%s", in, out, target);
return 0;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin);
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o,50,stdin);
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin);
return 1;
}
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char *data = NULL;
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(!feof(f))
{
fgets(data, sizeof(data), f);
p = strstr(data,t);
while (p != NULL)
{
c++;
p = strstr(p , t);
}
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}
修改
我对响应后的代码进行了一些更改,现在,我的程序在读取文件时崩溃了。新代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char data[1024];
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(fgets(data, sizeof(data), f))
{
fgets(data, sizeof(data), f);
p = strstr(data,t);
while (p != NULL)
c++; p = strstr(p+1 , t);
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin); i[strcspn(i, "\n")] = 0;
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o, 50, stdin); o[strcspn(o, "\n")] = 0;
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin); t[strcspn(t, "\n")] = 0;
return 1;
}
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
return 0;
}
答案 0 :(得分:2)
试试这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getIns(char *i, char *o, char *t);
int Search(char *i, char *o, char *t);
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
//printf("string entered: %s\n%s\n%s", in, out, target);
return 0;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin);
i[strcspn(i, "\n")] = 0;
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o,50,stdin);
o[strcspn(o, "\n")] = 0;
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin);
t[strcspn(t, "\n")] = 0;
return 1;
}
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char data[1024];
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(fgets(data, sizeof(data), f)){
p = strstr(data,t);
while (p != NULL)
{
c++;
p = strstr(p+1 , t);
}
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}