试图比较数组中的char并且它从不捕获C

时间:2015-09-11 23:21:21

标签: c arrays compare

我想要它做的是单词[count] ==','还是'。'去功能。当我在打印为a或a之前打印出[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*/

1 个答案:

答案 0 :(得分:1)

在调用feof()之前,始终需要检查read(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
}