[英]reading two arrays from a file
我是C编程的新手,我的一个程序有问题。 我应该从文件中读取三个数组。 我正在使用的文件是Temps.txt,下面是文件中的内容。
1 31 37
2 26 24
3 30 38
4 33 25
5 33 21
6 29 28
7 41 46
一直下降到左列31。我写的代码是
#include<stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
FILE *readfile;
int New_York[31];
int Anchorage[31];
int Dates[31];
int i;
printf( "Date New York Anchorage\n" );
if( ( readfile = fopen( "Temps.txt", "r" ) ) == NULL )
{
printf( "The file Temps failed to open\n" );
}
else
{
for( i = 0; i < 31; i++ )
{
fscanf( readfile, "%d, %d, %d", Dates + i, New_York + i, Anchorage + i );
printf( "%d %d %d\n", *(Dates + i), *(New_York + i), *( Anchorage + i ) );
}
if( fclose(readfile) == EOF ) //close the file.
{
printf("The file failed to close.\n");
}
}
return(0);
}
当我编译它时,它正在运行,并且它正在读取的所有数据都打印在Dates数组中,在其他两个数组中,我得到的确是正数和负数。 如果可以的话,我将不胜感激。
谢谢
删除逗号: "%d, %d, %d"
fscanf( readfile, "%d %d %d", Dates + i, New_York + i, Anchorage + i );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.