0

I'm getting bugs in my program (that is supposed to find all ints in and file of chars and ints) where "feof" doesn't work (while loop never ends) or no integers are read/found unless the entire file is full of ints.

My code...

 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>

  int main(void)
  {
   FILE *ptr;
   int val;
   int i = 0;
   char myChar;
   int chc;
   int result = 0;

     ptr = fopen("stuff.txt", "w");

     if(ptr == NULL)
       {
        printf("Could not open file \n");
       }

    for(; i < 5; ++i)
       {
        printf(" Char(0) or int(1)? ");
        scanf("%d", &chc);

       if(chc == 0)
         {
          printf(" Enter a char: ");
          scanf(" %c", &myChar);

          fprintf(ptr, " %c \n", myChar);
         }
       else
         {
          printf(" Enter an int: ");
          scanf("%d", &val);

          fprintf(ptr, " %d \n", val);
         }
      }

     fclose(ptr);

 FILE *rp;

 rp = fopen("stuff.txt", "r");

 if(ptr == NULL)
   {
    printf("Could not open file \n");
   }
 else
   {

    while(!feof(rp))
         {
           if(isdigit(fgetc(rp)))
             {
              ++result;
             }
          }
   }

  printf(" Total numbers in file: %d\n", result);


 return 0;
}

Edit:

I forgot to re-add "int result" and this is a new bug. Probably has to do with fgetc like someone mentioned. I get an extra one added to my total.

Solved: I'm reading 34 as "3" and "4", so it counts as two. Not sure how to marked as solved or if to delete this.

 Char(0) or int(1)? 0

 Enter a char: e

 Char(0) or int(1)? 1

 Enter an int: 34

 Char(0) or int(1)? 0

 Enter a char: p

 Char(0) or int(1)? 0

 Enter a char: d

 Char(0) or int(1)? 0

 Enter a char: u

 Total numbers in file: 2
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • http://stackoverflow.com/q/5431941/7761980 – ThingyWotsit May 20 '17 at 23:32
  • 1
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). This code is sort of an exception; when `fgetc()` returns EOF, the subsequent `feof(rp)` will be accurate, and you won't miscount EOF as a digit. – Jonathan Leffler May 20 '17 at 23:39
  • Do not reproduce. The loop ends. – BLUEPIXY May 20 '17 at 23:40
  • @JonathanLeffler fgetc must be why I get an extra one added to my total? I did see the question you referred. I tried "if(fscanf(rp, "%d", &v) == 1)" and it either hangs or doesn't end the loop. –  May 20 '17 at 23:59
  • 1
    `'3'` and `'4'` So `2`. – BLUEPIXY May 21 '17 at 00:08
  • 34 uses two digits, so the count should be 2. Using `scanf()` to read integers when there are letters in the input is not going to work well. – Jonathan Leffler May 21 '17 at 00:13
  • @BLUEPIXY I added a recent run where I only enter one number and it is counted as two numbers. Maybe fgetc and feof aren't a good combo? –  May 21 '17 at 00:13
  • `fgetc` read one character, not one `int`. – BLUEPIXY May 21 '17 at 00:16
  • It's two digits that make up `34`. You type two characters to enter it; `fgetc()` returns two separate digits — first `'3'` and then `'4'`. – Jonathan Leffler May 21 '17 at 00:16
  • @JonathanLeffler I see now, thanks! –  May 21 '17 at 00:20
  • @BLUEPIXY I see what you mean now, thanks! –  May 21 '17 at 00:21
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 May 22 '17 at 01:35
  • regarding the code block beginning with: `if(ptr == NULL)` 1) always place the literal on the left, so a keypunch error like: `if(ptr = NULL)` doesn't ruin your day. 2) error messages should be output to `stderr`, not `stdout` so use `perror()` rather than `printf()` Note: perror() will also output the reason the system thinks the error occurred. 3) there is no open file, so do NOT continue to execute the program, rather call `exit( EXIT_FAILURE); – user3629249 May 22 '17 at 01:39
  • what happens when the user is prompted with: `printf(" Char(0) or int(1)? ");` and the user enters: `3` or `a` or '\n'? I.E. NEVER trust the user to do the right thing, always validate the input (and if invalid, loop to try again – user3629249 May 22 '17 at 01:41
  • regarding: `scanf("%d", &val);` what happens if the user enters: `a` or '\n' or ```? I.E. always check the returned value from `scanf()` to assure it was successful. – user3629249 May 22 '17 at 01:43
  • when calling `fgetc()`, always check the returned value for not equal to EOF to assure the operation was successful. – user3629249 May 22 '17 at 01:45

1 Answers1

0

regarding the code block:

while(!feof(rp))
     {
       if(isdigit(fgetc(rp)))
         {
          ++result;
         }

This code block contains several problems, including the formatting is not easy to read. some of the problems have been detailed in the comments to the question. Suggest:

int ch;
while(EOF != (ch = fgetc(rp)) )
{
    if(isdigit(ch))
    {
        ++result;
    }
}

note that this counts every digit and you say your only interested in integers, which could be multiple digits, so you will need to modify the suggested code slightly. I'll let you do that modification.

user3629249
  • 16,402
  • 1
  • 16
  • 17