【发布时间】:2016-10-01 19:25:42
【问题描述】:
我有点困惑如何遍历数组并将每个字母添加到数组notes[] 中。我不确定是什么增加了 while 循环来扫描每个字符。我正在尝试传递每个字符以查看它是否是字母,然后将其大写。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(){
FILE * files;
char notes[1000];
int charcounter = 0, wordcounter = 0, c;
files = fopen("input.txt", "r");
if(!files)
{
return EXIT_FAILURE;
}
if(files)
{
while(fgets(notes, sizeof notes, files) != NULL)
{
size_t i, n = strlen(notes);
for (i = 0; i < n; i++)
{
if(isalpha(notes[i]))
{
int c = toupper(notes[i]);
putchar(c);
if(wordcounter == 50)
{
printf("\n");
wordcounter = 0;
}
if(charcounter == 5)
{
printf(" ");
charcounter = 0;
}
wordcounter++;
charcounter++;
}
}
}
}
fclose(files);
system("PAUSE");
return 0;
}
我用这个作为参考: 诠释 c;
FILE *file;
file = fopen("test.txt", "r");
if (file) {
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
}
【问题讨论】:
-
您将
toupper和isalpha应用于文件句柄!! -
提示:对于学习,最好从令人尴尬的简单任务开始。在您的代码中,您处理 2 个不同的方面:文件和数组。从仅涉及数组的任务开始。分而治之;)
-
我应用了文件句柄,我做错了吗? c 不是单独读取文件中的每个字符吗?
标签: c