私のCプログラムで、buffer[ ]
配列に格納されている文字列が出力されないという問題があります。printfがC言語で文字配列を出力しない
私のコードは次のとおりです。
#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;
}
最初printf
文が印刷されません、しかし、もう一つはありません。 これがなぜこのような理由で説明できるのですか?
[なぜ 'while(!feof(pToFile))'が間違っているのですか?(http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar
2番目の 'printf 'と呼ばれる?それは何をすべきか?入力(ファイル)とは何ですか?そして、「なぜwhile(!feof(file))」がいつも間違っているのはなぜですか?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) –
ああ、実際にデバッガを使用する方法を学ぶ必要がありますので、コードを一行ずつ進んで問題を簡単に見つけ出すことができます。 –