C中\n字符的诅咒
我制作了一个简单的拼写检查器,可以读取字典和用户文本文件来检查拼写。程序需要显示字典中没有的任何单词的行和单词索引。因此,在用户文本文件中(在段落或句子末尾)有一个返回字符之前,它可以正常工作。因此,Hello实际上是根据字典进行测试的,C中\n字符的诅咒,c,character,C,Character,我制作了一个简单的拼写检查器,可以读取字典和用户文本文件来检查拼写。程序需要显示字典中没有的任何单词的行和单词索引。因此,在用户文本文件中(在段落或句子末尾)有一个返回字符之前,它可以正常工作。因此,Hello实际上是根据字典进行测试的,Hello\n,程序认为它的拼写不正确。有人能建议删除\n字符的方法吗?这是我的密码: #include <stdio.h> #include <string.h> void StrLower(char str[]) { int
Hello\n
,程序认为它的拼写不正确。有人能建议删除\n
字符的方法吗?这是我的密码:
#include <stdio.h>
#include <string.h>
void StrLower(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
str[i] = (char)tolower(str[i]);
}
int main (int argc, const char * argv[]) {
FILE *fpDict, *fpWords;
fpWords = fopen(argv[2], "r");
if((fpDict = fopen(argv[1], "r")) == NULL) {
printf("No dictionary file\n");
return 1;
}
char dictionaryWord[50]; // current word read from dictionary
char line[100]; // line read from spell check file (max 50 chars)
int isWordfound = 0; // 1 if word found in dictionary
int lineCount = 0; // line in spellcheck file we are currently on
int wordCount = 0; // word on line of spellcheck file we are currently on
while ( fgets ( line, sizeof line, fpWords ) != NULL )
{
lineCount ++;
wordCount = 0;
char *spellCheckWord;
spellCheckWord = strtok(line, " ");
while (spellCheckWord != NULL) {
wordCount++;
spellCheckWord = strtok(NULL, " ,");
if(spellCheckWord==NULL)
continue;
StrLower(spellCheckWord);
printf("'%s'\n", spellCheckWord);
while(!feof(fpDict))
{
fscanf(fpDict,"%s",dictionaryWord);
int res = strcmp(dictionaryWord, spellCheckWord);
if(res==0)
{
isWordfound = 1;
break;
}
}
if(!isWordfound){
printf("word '%s' not found in Dictionary on line: %d, word index: %d\n", spellCheckWord, lineCount, wordCount); //print word and line not in dictionary
}
rewind(fpDict); //resets dictionarry file pointer
isWordfound = 0; //resets wordfound for next iteration
}
}
fclose(fpDict);
fclose(fpWords);
return 0;
}
#包括
#包括
void StrLower(字符str[])
{
int i;
对于(i=0;str[i]!='\0';i++)
str[i]=(char)tolower(str[i]);
}
int main(int argc,const char*argv[]{
文件*fpDict,*fpWords;
fpWords=fopen(argv[2],“r”);
if((fpDict=fopen(argv[1],“r”)==NULL){
printf(“无字典文件”);
返回1;
}
char dictionaryWord[50];//从字典读取的当前单词
字符行[100];//从拼写检查文件读取的行(最多50个字符)
int isWordfound=0;//如果在字典中找到单词,则为1
int lineCount=0;//我们当前所在的拼写检查文件中的行
int wordCount=0;//我们当前所在的拼写检查文件的第行有单词
while(fgets(line,sizeof line,fpWords)!=NULL)
{
lineCount++;
字数=0;
字符*拼写检查字;
拼写检查字=strtok(行“”);
while(拼写检查字!=NULL){
字数++;
拼写检查字=strtok(空,“,”);
if(拼写检查字==NULL)
继续;
StrLower(拼写检查字);
printf(“%s”\n”,拼写检查字);
而(!feof(fpDict))
{
fscanf(fpDict,“%s”,字典词);
int res=strcmp(字典词、拼写检查词);
如果(res==0)
{
isWordfound=1;
打破
}
}
如果(!isWordfound){
printf(“在第%d行的词典中找不到单词“%s”,单词索引:%d\n”,拼写检查单词,行数,字数);//打印不在词典中的单词和行
}
倒带(fpDict);//重置字典文件指针
isWordfound=0;//为下一次迭代重置wordfound
}
}
fclose(fpDict);
fclose(fpWords);
返回0;
}
哇,谢谢大家的快速回复。你们真是太棒了,带着它飞过了月球 尝试将\n添加到传递给strtok的参数。尝试将\n添加到传递给strtok的参数。如果您只是为了比较而删除字符,并且知道它将在一行末尾,那么当您将单词读入缓冲区时,请执行
strhr()
用于\n
,如果找到该位置,则将其替换为\0
。如果您只是为了比较而删除字符,并且知道它将位于一行的末尾,那么当您将该字读入缓冲区时,请执行strchr()
用于\n
,然后用\0
替换该位置(如果找到)。如何:
size_t length = strlen(dictionaryWord);
if (length > 0 && dictionaryWord[length-1] == '\n') {
dictionaryWord[length-1] = 0;
}
那么:
size_t length = strlen(dictionaryWord);
if (length > 0 && dictionaryWord[length-1] == '\n') {
dictionaryWord[length-1] = 0;
}
在调用
fgets()之后立即删除“\n”:
while ( fgets ( line, sizeof line, fpWords ) != NULL )
{
size_t linelen = strlen(line);
assert((linelen > 0) && "this can happen only when file is binary");
if (line[linelen - 1] == '\n') line[--linelen] = 0; /* remove trailing '\n' and update linelen */
在调用fgets()之后立即删除“\n”:
while ( fgets ( line, sizeof line, fpWords ) != NULL )
{
size_t linelen = strlen(line);
assert((linelen > 0) && "this can happen only when file is binary");
if (line[linelen - 1] == '\n') line[--linelen] = 0; /* remove trailing '\n' and update linelen */
或者,如果strtok
被抛弃(并非总是必要的,但我个人从未使用过),使用isspace
(或者可能!isalpha
)以避免任何进一步的空格冲突。strtok(line,“\n”)
不会从行中删除'\n'
,如果行仅“\n”
,或者,如果删除了strtok
(并非总是必要的,但我个人从未使用过),请使用isspace
(或者可能!isalpha
)以避免任何进一步的空格冲突。strtok(line,“\n”)
不会从行中删除'\n'
,如果行
只是“\n”
。这超出了问题的范围,但您的代码是错误的:它没有检查行的第一个单词,这是因为您在检查单词之前再次标记(使用strtok)。您应该在检查之后,在strtok当前所在的循环末尾执行此操作。这超出了问题的范围,但您的代码是错误的:它不会检查行的第一个单词,这是因为您在单词检查之前再次标记(使用strtok)。您应该在检查之后,在strtok当前所在的循环末尾执行。Nice:(我更喜欢dictionaryWord[--length]=0;
因为length
对以后的代码很有用。)。两者都完成了任务。很好:(我更喜欢dictionaryWord[--length]=0;
,因为length
对以后的代码很有用。)。两者都完成了任务。fgets()
的结果并不总是以“\n”
结尾。长的行只读了一部分就不会有了。文件中的最后一行也可能不存在strchr()
在添加返回值为NULL
的测试时起作用。fgets()
的结果并不总是以“\n”
结束。长的行只读了一部分就不会有了。文件中的最后一行也可能不存在strchr()
在添加返回值为NULL
的测试时起作用。