#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
//=====================================================
//基本数据
const int startlon = 9755;//起始经度(97.55*100)
const int startlat = 3995;//起始纬度
const int xrolls = 2; //行数
const int yrolls = 4;//列数
const int step = 10;//步长(0.1*100)
const int filenumber = 3;//文件总个数
float data[xrolls][yrolls];
//======================================================
//文件操作
char infilename[100];
FILE* fout,*fin;
fout = fopen("F:\\2017C\\test.txt", "a+");
int dlon, dlat;
//对纬度,对经度循环
for (dlat = 0; dlat < xrolls; dlat++){
for (dlon = 0; dlon <yrolls; dlon++){
float lon = (dlon*step + startlon)*0.01;
float lat = (dlat*step + startlat)*0.01;
fprintf(fout, "%6.2f""%6.2f", lon, lat);
//对文件开始循环
for (int filecount = 1; filecount <= filenumber; filecount++){
sprintf(infilename, "F:\\2017C\\test%d.txt", filecount);//读入位置!!修改
fin = fopen(infilename, "r");
if (!infilename){
printf("could not open file!");
exit(99);
}
//读入数据
while (!feof(fin))
{
for (int i = 0; i < xrolls; i++){ //i<行数
for (int j = 0; j < yrolls; j++){ //j<列数
fscanf(fin, "%f", &data[i][j]);
}
}
}
//写入数据
if (filecount == filenumber)fprintf(fout, "%6.2f\n", data[dlat][dlon]);//如果是横排最后一个,则输入回车
else fprintf(fout, "%6.2f", data[dlat][dlon]);//(行,列)==(纬度,经度)
fclose(fin);
}
}
}
fclose(fout);
return 0;
}
1.11 2.22 3.33 4.44
5.55 6.66 7.77 8.88
//testx.txt
//lon lat Jan Feb Mar............
97.55 39.95 0.00 1.11 3.11
97.65 39.95 1.00 1.22 3.22
97.75 39.95 2.00 1.33 3.33
97.85 39.95 3.00 1.44 3.44
97.55 40.05 4.00 2.11 3.55
97.65 40.05 5.00 2.22 3.66
97.75 40.05 6.00 2.33 3.77
97.85 40.05 7.00 2.44 3.88
//test.txt
热门评论
其中fscanf函数,float配合"%f",double配合"%lf"。不需要按数据宽度,保留小数点之类格式读入