我想知道是否有人能就我遇到的问题给我一些建议。下面的代码读取一个预先编写的"输入文件"(在我的代码中名为"input"(,然后复制其内容并创建一个新文件(在我代码中称为"output1"(。
我想把这个";output1";阵列(ch(相对于";"停止语";数组(停止(并删除所有常用字;output2";文件和结果。
我该怎么做?我的代码目前发布在下面。
#include <stdio.h>
int main(void)
{
FILE *read;
FILE *write;
FILE *stopword;
char ch[100];
char stop[100];
read = fopen("input.dat", "r");
write = fopen("output1.dat", "w");
stopword = fopen("stopword.dat", "r");
while (fgets(ch, 100, read))
{
fputs(ch, write);
printf("%s", ch);
}
printf("n");
while (fgets(stop, 100, stopword))
{
printf("%s", stop);
}
fclose(read);
fclose(write);
fclose(stopword);
return 0;
}
input.dat的内容为:
In this program, you are hoping to remove the common words.
A result there should display something which will remove all of common words,
and can be placed in a new output2 file.
stopword.dat的内容为:
in this you are to the a there which will of and can be
所需的输出2.dat应为:
program hoping remove common words result should display something remove all common words placed new output2 file
如有任何帮助,我们将不胜感激。
以下是您应该实现的步骤:
- 阅读停止单词列表,每次一个单词。对于每个单词:
- 将副本存储到内存中的字符串数组中
- 一次读取一个单词的输入文件。对于每个单词:
- 将单词输出到output1.dat
- 如果在停止单词列表中找不到该单词,请将该单词输出到output2.dat
- 完成
要一次读取一个单词,可以使用fscanf()
,但文件input.dat中存在的标点符号将被fscanf
的%s
转换视为单词的一部分。因此,我建议一次读取一个字节,并将单词检测为字母、数字和破折号序列。