计算文本文件中的字数,并在c/c+中的不同文本文件中打印结果+;
守则:计算文本文件中的字数,并在c/c+中的不同文本文件中打印结果+;,c,file,count,C,File,Count,守则: #include <ctype.h> #include <stdio.h> #include <string.h> char filename[] = "11.txt"; char filename1[] = "2.txt"; FILE *ptr, *resultptr; char string[100]; char words[100][100]; int len = sizeof(filename) / s
#include <ctype.h>
#include <stdio.h>
#include <string.h>
char filename[] = "11.txt";
char filename1[] = "2.txt";
FILE *ptr, *resultptr;
char string[100];
char words[100][100];
int len = sizeof(filename) / sizeof(char);
int i = 0, j = 0, k, length, count;
int main()
{
fopen_s(&ptr, filename, "r");
fopen_s(&resultptr, filename1, "w");
if ((ptr == nullptr) || (resultptr == nullptr)) {
printf("Files were not opened!");
return -1;
}
while (fgets(string, sizeof string, ptr)) {
for (k = 0; string[k] != '\0'; k++) {
if (string[k] != ' ' && string[k] != '\n') {
words[i][j++] = tolower(string[k]);
} else {
words[i][j] = '\0';
i++;
j = 0;
}
}
length = i + !!j;
fputs("Occurrences of each word:\n", resultptr); //prints this sentence into file
for (i = 0; i < length; i++) {
if (strcmp(words[i], "0") == 0)
continue;
count = 1;
char *ch = words[i];
for (j = i + 1; j < length; j++) {
if (strcmp(words[i], words[j]) == 0 && (strcmp(words[j], "0") != 0)) {
count++;
strcpy_s(words[j], "0");
}
}
fputs("The word ", resultptr);
if (string[i] != ' ' && string[i] != '\n') {
fprintf(resultptr, "%s", ch);
}
fputs(" occurred ", resultptr);
fprintf(resultptr, "%d", count);
fputs(" times\n", resultptr);
}
fclose(ptr);
fclose(resultptr);
return 0;
}
}
怎么了?好像我不专业,但有人能告诉我这里出了什么问题吗?我对原来的问题做了一些修改,但仍然有很多错误。注意:与此同时,OP对问题进行了上述修复,从而使这个答案无效。此答案适用于。 怎么了
单词0出现了1次
-您选择将重复的单词替换为字符串“0”
。为了不将这些替换项计算为单词,请插入
if (strcmp(words[i], "0") == 0) continue;
在打印循环体的开始处。似乎您打算if(string[i]!='&&string[i]!='\0'&&string[i]!='0')
这样做,但这不起作用-删除此代码。此外,空字符串将是更好的选择,允许单词
0
这个词
发生1次
-结尾处的'\n'
被算作一个单词。为了不计算此值,并跳过标点符号,以及避免由于连续的非单词字符而出现空单词,请替换
if (string[k] != ' ' && string[k] != '\0') {
words[i][j++] = tolower(string[k]);
}
else
与
该单词出现了1次
-对文件末尾的一个空单词进行了计数。为了不计算这些,仅当在EOF,i的一个单词内时,才向i加1。E改变
length = i + 1;
到
代码中存在多个问题:
- 全局变量应移动到
函数的主体内main()
不可移植,请改用fopen_s()
fopen()
不可移植,请改用strcpy\u s()
,或者如果字符串为strcpy()
,则只需将第一个字节设置为空字符串“\0”
和i
应在每次j
fgets()之后重置为
0
- 您应该使用
测试字母,而不是只测试空格和换行符isalpha()
- 您应该通过将重复的单词设置为空字符串来清除它们
- 您应该对输出行使用一个简单的
调用fprintf()
- 您不应该关闭
循环中的文件while(fgets(…)
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(disable:4996) // disable Microsoft obnoxious warning
#endif
#define WORDS 2000
#define CHARS 40
int main() {
char filename[] = "11.txt";
char filename1[] = "2.txt";
FILE *ptr, *resultptr;
char string[100];
char words[WORDS][CHARS];
int i, j, k, length, count;
ptr = fopen(filename, "r");
if (ptr == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return 1;
}
resultptr = fopen(filename1, "w");
if (resultptr == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename1, strerror(errno));
return 1;
}
i = j = 0;
while (i < WORDS && fgets(string, sizeof string, ptr)) {
for (k = 0; string[k] != '\0'; k++) {
unsigned char c = string[k];
if (isalpha(c)) {
if (j < CHARS - 1)
words[i][j++] = tolower(c);
} else {
words[i][j] = '\0';
if (j > 0) {
j = 0;
i++;
if (i == WORDS)
break;
}
}
}
if (j > 0) {
// include the last word if the file does not end with a newline
words[i][j] = '\0';
i++;
}
}
length = i;
fprintf(resultptr, "Occurrences of each word:\n");
for (i = 0; i < length; i++) {
if (words[i][0] == '\0')
continue;
count = 1;
for (j = i + 1; j < length; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
words[j][0] = '\0';
}
}
fprintf(resultptr, "The word %s occurred %d times\n", words[i], count);
}
fclose(ptr);
fclose(resultptr);
return 0;
}
#包括
#包括
#包括
#包括
#ifdef硕士学位
#pragma警告(disable:4996)//禁用Microsoft讨厌的警告
#恩迪夫
#定义单词2000
#定义字符40
int main(){
字符文件名[]=“11.txt”;
char filename1[]=“2.txt”;
文件*ptr,*resultptr;
字符串[100];
字符字[字][字符];
int i,j,k,长度,计数;
ptr=fopen(文件名,“r”);
如果(ptr==NULL){
fprintf(stderr,“无法打开%s:%s\n”,文件名,strerror(errno));
返回1;
}
resultptr=fopen(filename1,“w”);
if(resultptr==NULL){
fprintf(stderr,“无法打开%s:%s\n”,文件名1,strerror(errno));
返回1;
}
i=j=0;
while(i0){
j=0;
i++;
if(i==单词)
打破
}
}
}
如果(j>0){
//如果文件未以换行符结尾,请包含最后一个单词
字[i][j]='\0';
i++;
}
}
长度=i;
fprintf(resultptr,“每个单词的出现次数:\n”);
对于(i=0;i
这是因为您只调用了一次fgets
。要改变这一点,请替换fgets(字符串,100,ptr)//读取一行while(!feof(ptr)){
和while(fgets(string,sizeof string,ptr))
并删除末尾多余的}
。如果它打印单词0,则似乎您尚未进行更改1。由于无法正确格式化注释中的数据,您最好将输入文件的真实内容插入问题帖子的“```行之间。将其放在(i=0;i之后{
-我现在走了,两小时后回来,处理其他问题-如果你那时还没有自己解决这些问题的话。-)例如,内置的单词应该算多少?评论不用于扩展讨论;此对话已经结束。请不要更改问题,使现有的答案无效。I h已回滚您最近的编辑。如果您想用后续问题更新您的问题,则可以将其添加到问题的底部。只要原始问题保持不变,这不会使现有答案无效。@Youngwildandfree:您可以通过单击其分数下的灰色复选标记来接受其中一个答案。
length = i + !!j;
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(disable:4996) // disable Microsoft obnoxious warning
#endif
#define WORDS 2000
#define CHARS 40
int main() {
char filename[] = "11.txt";
char filename1[] = "2.txt";
FILE *ptr, *resultptr;
char string[100];
char words[WORDS][CHARS];
int i, j, k, length, count;
ptr = fopen(filename, "r");
if (ptr == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename, strerror(errno));
return 1;
}
resultptr = fopen(filename1, "w");
if (resultptr == NULL) {
fprintf(stderr, "cannot open %s: %s\n", filename1, strerror(errno));
return 1;
}
i = j = 0;
while (i < WORDS && fgets(string, sizeof string, ptr)) {
for (k = 0; string[k] != '\0'; k++) {
unsigned char c = string[k];
if (isalpha(c)) {
if (j < CHARS - 1)
words[i][j++] = tolower(c);
} else {
words[i][j] = '\0';
if (j > 0) {
j = 0;
i++;
if (i == WORDS)
break;
}
}
}
if (j > 0) {
// include the last word if the file does not end with a newline
words[i][j] = '\0';
i++;
}
}
length = i;
fprintf(resultptr, "Occurrences of each word:\n");
for (i = 0; i < length; i++) {
if (words[i][0] == '\0')
continue;
count = 1;
for (j = i + 1; j < length; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
words[j][0] = '\0';
}
}
fprintf(resultptr, "The word %s occurred %d times\n", words[i], count);
}
fclose(ptr);
fclose(resultptr);
return 0;
}