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?

7
  • 4
    why while(!feof(pToFile)) is wrong Commented Oct 10, 2016 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?. Commented Oct 10, 2016 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. Commented Oct 10, 2016 at 17:28
  • 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. Commented Oct 10, 2016 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. Commented Oct 10, 2016 at 17:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.