c - 试图比较数组中的字符,但它永远不会捕获 C

标签 c arrays compare

我想让它做的是,如果 word[count] == ',' 或 '.'去功能。当我在打印为 a 或 a 之前打印出 word[count] 的正确位置时。但它永远不会发挥作用。我什至尝试将其存储为 char 并进行比较,但是,当我将其存储为 char c 并打印时,我得到的只是一个 ' '.....(空白)这里的问题是什么。

#include<stdio.h>

void print_word(char word[], int count);
void special_char();
void fill(char word[], int count);

int main(){
FILE *fp;
     char ch;
     char word[50];
     int count = 0;
     int boolean_comma = 0;
     fp = fopen("./../instr/lab1.dat","r");

        if( fp == NULL) {
                 printf("ERROR");
        }else
                while(!feof(fp)){/*while pointer hasnt reached end of file continue loop*/
                        count++;
                   /*gets rid of the array jump, allowing it to reinitilize back to zero only at the beginning of each word*/
                        ch = fgetc(fp);
                        word[count] = ch;       /*inset ch into array in coordinance with the count*/
                /*      printf("count: %i, char: %c\n",count,ch);*/
                if(ch == ' ' || ch == '\n'){ /*Singals end of word*/
                                /*if last character is space and word[count-1] != ',' or a '.'   */
                                /*then print out word normally, but if the [!=] above is correct */
                                /*And the count is equal to for fill the first 4 of the array    */
                                /*with '*'s'|| but if word[count-1] == ',' or a '.' and the count*/
                                /*is == 5; then send to special char, which will fill first four */
                                /*of the array with '*'s' if the count is other than 5 sned it   */
                                /*to the normal print_word function and allow it to print.        */
                        if(ch == '\n'){count-=1;}
                        print_word(word,count); /*Sends array of words with count to print char by char*/
                        printf(" "); /*print space to separate words*/
                        count = 0;
                }

                }/* END while FEOF*/



fclose(fp);
return 0;

}//end main

/*This function will print all words, if the count is 4 it will fill with ASTRIK*/
/*The problem here is if the count is 4 and the fourth character is a special ch*/
/*to solve this problem check word[count] if it is equal to a ',' or '.' it     */
/*must then bypass the normal for loop and go to a different one that will fill */
/*with the ASTRIKS and a COMMA or PERIOD                                        */

void fill(char word[], int count)
{
int a;
         for(a = 1; a <= count; a++){
         word[a] = '*';

         }/*END FOR*/


}/*END FILL*/

void print_word(char word[],int count)
{
int a;
char c = word[count];

count-=1;/*Gets rid of the count value which is a ' '*/
if(word[count] == ' '){count-=1;}/*gets rid of space*/
        printf("count: %i ",count);
        printf("[c:%c][w[:%c]\n",c,word[count]);
        if(word[count] !=( ',' || '.')){
        /*count-=1;*/

                if( count == 4){
                fill(word,count); /*Calls method fill*/

                }/*END IF*/

        }/*END IF*/

         if(word[count] == (',' || '.')){
                printf("here----------------\n");
                if(count == 5){
                fill(word,count); /*Calls the fill method with a -1*/

                }/*END IF*/

        }/*END ELSE IF*/

                 /*FOR LOOP wil end the function by printing edited word or unedited word*/
                 for(a = 1; a <= count; a++){
                 printf("%c",word[a]);

                }/*END IF*/


                printf("--%c", word[count]);
}/*end print_word*/

void speical_char(char word[],int count){


}/*end speical_char*/

最佳答案

在调用 feof() 之前,始终需要检查读取的返回值(fread()、fscanf() 或 fgetc())。

就像它进入循环的次数比你预期的多。如果出现读取错误,循环永远不会终止。

尝试:

int c;

while ((c = fgetc(fp)) != EOF) {
    // do something with c
}
if (ferror(fp)) {
    // handle the error, usually exit or return
} else {
    // continue execution
}

关于c - 试图比较数组中的字符,但它永远不会捕获 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32533461/

相关文章:

c - 带有 arm64 交叉编译器的 gcc 插件 : cannot open shared object file

c - 位操作 :print the next smallest and largest numbers with same no of 1 bits

c++ - 如何让Makefile在一个目录下编译多个c文件和h文件

javascript - 将国家代码转换为国家全名

ruby - 用 7n +1 填充 ruby​​ 中的数组

java - 大学 Java 练习(字符串)遇到问题

c - Glibc 和 uClibc 并排在一个系统上

python - 扁平化numpy数组,还能保留值位置的索引?

ios - if(NSOrderedAscending == result) 有人能解释一下吗

rust - 使用对比较运算符或函数的引用来比较某些值