我试图读取包含结果表的文件,并将表标题存储为数组中的单独字符串。例如,该表有6个标题,我想将每个标题存储在数组的字段中:标题[6]
遇到一些困难。该程序要么崩溃要么不做任何事情。以下是代码的两种变体:
#include <stdio.h>
int main()
{
FILE *weatherData = 0;
weatherData = fopen("weatherData.txt", "r");
char str, titles[56];
int i, count;
int Date[32], High[32], Low[32], Precip[32], Snow[32];
if (weatherData == 0)
{
printf("File did not open. Check code and retry");
}
/* for (i=0; i<6; i++)
{
fscanf(weatherData, "%c", titles[i]);
printf("%c ", titles[i]);
}*/
while (str != '\n')
{
fscanf(weatherData, "%s", titles);
printf("%s ", titles);
}
fclose(weatherData);
return 0;
}
此代码打印一个0的无结束ammount ...并且标记的代码(/ ** /)只会使程序崩溃。有帮助吗?问题肯定在于循环,只是无法弄清楚如何使其发挥作用。
答案 0 :(得分:0)
for (i=0; i<6; i++)
{
fscanf(weatherData, "%s", titles);
printf("%s ", titles);
}
printf("\n");