1

what i want it to do is if word[count] == ',' or a '.' to go to the function. when i print out what word[count] is right before it prints as a , or a . BUT it never goes to the function. i even tried to store it as a char and compare but, when i stored it as char c and printed all i was given was a ' '..... (empty space) what the problem here.

#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*/
cmehmen
  • 249
  • 1
  • 3
  • 12

1 Answers1

1

It is always necessary to check the return value of a read (either an fread(), or an fscanf(), or an fgetc()) before calling feof().

like it enters the loop one more time than you expect. If there is a read error, the loop never terminates.

Try:

int c;

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