首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用fgetc读取某些字符

用fgetc读取某些字符
EN

Stack Overflow用户
提问于 2014-09-24 15:52:21
回答 1查看 708关注 0票数 1

我试图创建一个拼写检查程序,它接受一个输入文件,并通过搜索字典文件来确保每个单词是正确的。我面临的问题是,当我试图从输入文件中用空格分隔每个单词并将其放入char []中时,由于某种原因,使用"的单词会打印出来。

代码语言:javascript
复制
H0
i1
c0
h1
r2
i3
s4
!5
â0
1
2
h3
o4
w5
w6
â7
8
9
a0
r1
42
e3
y0
o1
u2
.3

整数是我的索引

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dict.h"

int main(int argc, char *argv[] ) {
    FILE *fdict,*input;
    int i;
    char ch;

/* the biggest posible word is 30 plus a possible of two " or ' characters and the null character. so the limit of the array is 33*/ 
    char norm[33];

    if ( argc < 3 ) /* argc should be 3 for correct execution*/
    {
        fprintf(stderr,"1 or 2 Files were missing.");
        exit(1);
    }

    if ( argc > 3 ){
        fprintf(stderr,"too many Arguments");
        exit(1);
    }

    /* We assume argv[1] and agrv[2] are filenames to open*/
    fdict = fopen( argv[1], "r" );/* file pointer for the dictionary file*/
    input = fopen( argv[2], "r" );/*file pointer for the input file*/

    /* fopen returns NULL on failure */
    if ( fdict == NULL ){
        fprintf(stderr,"Could not open file: %s\n", argv[1] );/*checks to make sure the dictionary file can be opened*/
        exit(1);
    }

    if ( input == NULL ){
        fprintf(stderr,"Could not open file: %s\n", argv[2] );/*checks to make sure the input file can be opened*/
        exit(1);
    }
            /* Read one character at a time from file, stopping at EOF, which
                indicates the end of the file. Note that the idiom of "assign
                to a variable, check the value" used below works because
                the assignment statement evaluates to the value assigned. */
    while ( ( ch = fgetc( input ) ) != EOF ) {          

        char word[33] = "";/* resets the array*/

        for ( i = 0; !isspace( ch ) ; i++ ){
            word[i] = ch;
            printf("%c%d\n",ch,i);/* checking to see what is wrong with the index*/
            ch = fgetc( input );

        }


    }
    fclose( fdict );
    fclose( input );

    return 0;

}

我的输入看起来是:

代码语言:javascript
复制
Hi chris! “howw” are you.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-24 16:00:05

"不一样。(3个不同的引号)基于不同的编码,这三个字符使用不同的char序列来表示它们,而代码一次只打印一个char

建议只使用simpe引号标记"

一个简单的或程序员的文本编辑器就可以了。避免使用可能会引入非ASCII引号的字处理器,直到代码准备就绪(@n.m.)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26021322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档