0

I have a problem with my C program not outputting the string stored in my buffer[ ] array.

My code is:

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

int main()
{
  int textSize = 20;

  int index, ch;
  int count = 0;
  int upperCount = 0;
  int lowerCount = 0;
  char buffer[textSize];

  FILE *pToFile = fopen("input.txt", "r");   

  if (pToFile != NULL)
  {
    while (!feof(pToFile))
    {    
      /* Read in a single line from "stdin": */
      for(index = 0; (index < textSize) && ((ch = fgetc(pToFile)) != EOF)
                      && (ch != '\n'); index++) {
        if(islower(ch))
        {
          buffer[index] = toupper(ch);
          count++;
          upperCount++;
        }
        else if(isupper(ch))
        {
          buffer[index] = tolower(ch);
          count++;
          lowerCount++;
        }
        else
        {
          buffer[index] = (char)ch;
          count++;
        }
      }
    }
  }
  fclose(pToFile);      

  /* Terminate string with null characters: */
  buffer[index] = '\0';

  /* Print output out onto screen */
  printf("%s\n", buffer);
  printf("Read %d characters in total, %d converted to upper-case, %d to lower-case\n", 
                             count, upperCount, lowerCount);
  return 0;
}

The first printf statement does not print, however the second one does. Please could anyone help explain why this is the case?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Zaya
  • 1
  • 1
  • 4
    [why `while(!feof(pToFile))` is wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Oct 10 '16 at 17:26
  • What does the second `printf` called? Is the output of that what it should be? What is the input (the file)? And please read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – Some programmer dude Oct 10 '16 at 17:27
  • Oh, and you should really learn how to use a debugger so you step through the code line by line and then be able to easily find out the problem yourself. – Some programmer dude Oct 10 '16 at 17:28
  • I see no reason to believe that the first `printf` fails to print, but I could believe that it does not print what you expect. I could especially believe that it prints zero or more non-printing characters (maybe spaces and/or tabs) followed by a newline. – John Bollinger Oct 10 '16 at 17:33
  • 3
    Every time you start processing a new line, you set `index` back to `0`. When you finally read `EOF` after the last newline, `index = 0`, so you do `buffer[0] = '\0';`, so there's nothing to print. – Barmar Oct 10 '16 at 17:33
  • 1
    The first `printf` will only print something if the file doesn't end with a newline. Then it will print that last line. – Barmar Oct 10 '16 at 17:34
  • `index < textSize` --> `index + 1 < textSize` – chux - Reinstate Monica Oct 10 '16 at 17:41

1 Answers1

1

The problem is your with your loops, especially that while (!feof(pToFile)) loop.

Lets say your text contains a single line line less than 19 characters long, that is terminated by a newline. That last bit, the line ending in a newline is important.

What happens when you read the file is that you encounter the newline, break the inner for loop and you are back in the outer loop. Because we have not passed the end of the file yet feof(pToFile) will return false, and you go back to your for loop.

This time in the for loop the very first time you call fgetc it will notice that you are at the end of the file and return EOF and you break out of the loop. However, because your initializing expression in the for loop is index = 0 you will exit the loop with index being equal to zero.

Now the file is at its end, and feof(pToFile) will return true, and you exit the outer loop, and then you terminate the string in buffer with index being zero, i.e. you do

buffer[0] = '\0';

Now you have an "empty" string that you print.

The simple solution? Skip the outer while loop, you don't need it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621