基于字符串比较字的翻译程序不能正常工作 [英] Word translator program based on string comparison is not working properly
问题描述
我做了一个程序,它从文件中读取 english_dictionary.txt
的英语100个最常用的单词,并在 foreign_dictionary.txt
我把各个单词的翻译是一门外语。
在 .TXT
该文件的内容被放在两个的char *数组[100]
。
然后程序从文件中读取 text_to_translate.txt
,即包含英文文本,将取代英文字符串(字)与国外的,如果它发现一个匹配。但是,这是行不通的,有人能告诉我为什么吗?
下面是我的code:
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;诠释主要(无效){ 字符*外资[100];
INT I; FILE *计划生育=的fopen(foreign_dictionary.txt,R); 对于(I = 0; I&小于100 ++ⅰ){
外资[I] =的malloc(15 * sizeof的(炭));
与fgets(外由[i],20,FP);
}
FCLOSE(FP); 字符*英语[100]; FP = FOPEN(english_dictionary.txt,R); 对于(I = 0; I&小于100 ++ⅰ){
英语[i] =的malloc(15 * sizeof的(炭));
与fgets(英文[I],20,FP);
}
FCLOSE(FP);
// ------------------------如果找到一个匹配,并打印翻译--------
字符*缓冲=的malloc(15 * sizeof的(炭));
INT标志= 0;
FP = FOPEN(text_to_translate.txt,R);
而(与fgets(缓冲液,20,FP)!= NULL){
对于(I = 0; I&小于100 ++ⅰ)
如果(英文[I] ==缓冲区){
的printf(%S,对外[I]);
标志= 1;
}
如果(标志== 1)
继续;
其他
的printf(%S,缓冲区);
} 返回0;
}
P.S。如果需要的话,该词典文件由这样:
的
和
至
的
。
。
。
-
首先在code,你在做
英语[I] ==缓冲
比较字符串,但使用
==
运营商无法比拟的字符串。您需要使用的strcmp()
代替。 -
二
与fgets( )
读取并存储尾随换行
。它始终是更好地手动剥离它们赶走,大部分时间它们会导致失败的字符串的比较。 -
最后你在分配
15
字节到你提供给与fgets缓冲区( )
而与fgets的最大读数限度()
19(1小于提供的)。这可以创建未定义行为,如果输入的是有超过14字符
秒。此外的sizeof(char)的
是保证生产1
在C
。你能把那一部分。
建议字:对于这个程序的设计,我觉得还有一个错误。你从来没有检查的与fgets成功()
,只读取文件无条件为100次,然后再使用这些值。这会造成两个问题。
- 有些内容可以有效地成为
NULL
。所以,以后在使用这些值,再次你要面对UB。 - 如果您正在为循环固定数量的具有固定大小的,恕我直言迭代的,不存在的需求的使用动态内存分配。
改变你的方法有点像
- 创建足够长的一个临时缓冲区数组。
- 使用
与fgets读输入到该临时阵列()
- 检查
与fgets()
成功(非 -NULL
返回值)搜索结果- 的如果没有
NULL
的搜索结果- 剥去最后
换行
输入 - 计算容纳这个字符串所需的长度
- 分配动态内存的指针数组
- 拷贝输入
- 剥去最后
- 的如果
NULL
的,搜索结果- 是指档案结尾,没有什么更多的阅读,这样你就可以走出循环来读取该特定文件的投入。
- 的如果没有
I made a program that reads from file english_dictionary.txt
the 100 most frequently used words in the English language, and in foreign_dictionary.txt
I put the translations of the respective words in a foreign language.
The content of the .txt
files is placed in two char *array [100]
.
Then the program reads from file text_to_translate.txt
, that containing a text in English, and would replace the strings(words) in English with foreign ones if it finds a match. But it does not work, someone can tell me why?
Below is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *foreign[100];
int i;
FILE *fp = fopen("foreign_dictionary.txt", "r");
for (i = 0; i < 100; ++i){
foreign[i] = malloc(15 * sizeof(char));
fgets(foreign[i], 20, fp);
}
fclose(fp);
char *english[100];
fp = fopen("english_dictionary.txt", "r");
for (i = 0; i < 100; ++i){
english[i] = malloc(15 * sizeof(char));
fgets(english[i], 20, fp);
}
fclose(fp);
//------------------------if find a match and print the translation--------
char *buffer = malloc(15 * sizeof(char));
int flag = 0;
fp = fopen("text_to_translate.txt", "r");
while (fgets(buffer, 20, fp) != NULL){
for (i = 0; i < 100; ++i)
if (english[i] == buffer){
printf("%s", foreign[i]);
flag = 1;
}
if (flag == 1)
continue;
else
printf("%s", buffer);
}
return 0;
}
P.S. If required, the dictionaries files are made so:
the
and
to
of
.
.
.
Firstly in your code, you're doing
english[i] == buffer
to compare the strings but you cannot compare strings using
==
operator. You need to usestrcmp()
instead.Second ,
fgets()
reads and stores the trailingnewline
. It is always better to strip them off manually, most of the time they cause failure in string comparison.Lastly you're allocating
15
bytes to the buffer which you're supplying tofgets()
while the maximum reading limit forfgets()
is 19 (1 less than supplied). This can create undefined behaviour by buffer overrun, if the input is having more than 14char
s. Moreoversizeof(char)
is guaranteed to produce1
inC
. You can just drop that part.
Word of advice: Regarding the design of this program, I feel there is one more bug. You never check for the success of fgets()
, just reading the files unconditionally for 100 times and later using those values. This can create two issues.
- Some of the content can effectively become
NULL
. So, later while using those values, again you'll face UB. - If you're looping for fixed number of iterations with fixed size, IMHO, there is no requirement for using dynamic memory allocation.
Change your approach a bit, like
- Create one temporary buffer array of enough length.
- Read the input to that temp array using
fgets()
- Check for
fgets()
success (non-NULL
return value)
- if not
NULL
- strip the input of the last
newline
- calculate the length required to hold the string
- allocate dynamic memory to the pointer array
- copy the input
- strip the input of the last
- if
NULL
,
- it refers end-of-file, there is nothing more to read, so you can come out of the loop to read the input for that particular file.
- if not
这篇关于基于字符串比较字的翻译程序不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!