简体   繁体   English

printf在C中不输出char数组

[英]printf does not output char array in C

I have a problem with my C program not outputting the string stored in my buffer[ ] array. 我的C程序无法输出存储在buffer[ ]数组中的字符串,这是一个问题。

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. 第一个printf语句不打印,但是第二个打印。 Please could anyone help explain why this is the case? 请任何人帮助解释为什么会这样吗?

The problem is your with your loops, especially that while (!feof(pToFile)) loop. 问题出在您的循环中,尤其是while (!feof(pToFile))循环时。

Lets say your text contains a single line line less than 19 characters long, that is terminated by a newline. 假设您的文字包含一行少于19个字符的单行,并以换行符结尾。 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. 读取文件时发生的情况是遇到换行符,中断了内部for循环,而您又回到了外部循环中。 Because we have not passed the end of the file yet feof(pToFile) will return false, and you go back to your for loop. 因为我们尚未传递文件的末尾,但feof(pToFile)将返回false,然后返回到for循环。

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. 这次在for循环中,第一次调用fgetc它会注意到您位于文件末尾并返回EOF并且退出循环。 However, because your initializing expression in the for loop is index = 0 you will exit the loop with index being equal to zero. 但是,由于您在for循环中的初始化表达式是index = 0您将退出index等于零的循环。

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, ie you do 现在文件在其末尾, feof(pToFile)将返回true,退出外部循环,然后在index为零的缓冲区中终止字符串,即

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. 跳过外部的while循环,则不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM