用C语言将txt文件读入后,如何存入不同的数组,并输出在屏幕上
CUST NO. XCOORD. YCOORD. DEMAND READY TIME DUE DATE SERVICE TIME
1 35.00 35.00 0.00 0 .00 230.00 0.00
2 41.00 49.00 10.00 161.00 171.00 10.00
3 35.00 17.00 7.00 50.00 60.00 10.00
4 55.00 45.00 13.00 116.00 126.00 10.00
5 55.00 20.00 19.00 149.00 159.00 10.00
6 15.00 30.00 26.00 34.00 44.00 10.00
7 25.00 30.00 3.00 99.00 109.00 10.00
8 20.00 50.00 5.00 81.00 91.00 10.00
这样的数据有100行我已经将txt文件读进去了,但是存入数组失败,我的源程序如下:
#define N 101
#define M 2
#include<stdio.h>
#include <stdlib.h>
#include<process.h>
main()
{ /*变量定义*/
int i;
int num;
int nDemand[100];
int nTimewindowE[N];
int nTimewindowL[N];
double dCoordinateX[N];
double dCoordinateY[N];
double dDistance[N][N];
double dServicetime[N];
int nRoutes[20][N];
int bFlag[N];
int capacity=200;
FILE *fp;
fp=fopen("R101.txt","r");
if(fp==NULL)
{
printf("file open erro!\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%d%f%f%d%d%d%f",&num,dCoordinateX,dCoordinateY,nDemand,nTimewindowE,nTimewindowL,dServicetime);
fprintf(stdout,"%d%f%f%d%d%d%f",num,dCoordinateX,dCoordinateY,nDemand,nTimewindowE,nTimewindowL,dServicetime);
}
fclose(fp);
} 展开
#include<stdio.h>
#include <stdlib.h>
#include<process.h>
#include<Windows.h>
#define N 101
#define M 2
int main()
{ /*变量定义*/
int i;
int num;
double nDemand;
double nTimewindowE;
double nTimewindowL;
double dCoordinateX;
double dCoordinateY;
double dDistance[N][N];
double dServicetime;
int nRoutes[20][N];
int bFlag[N];
int capacity=200;
FILE *fp;
fp=fopen("R101.txt","r");
if(fp==NULL)
{ printf("file open erro!\n");
// exit(0);
}
while((!feof(fp)))
{
fscanf(fp,"%d %lf %lf %lf %lf %lf %lf",&num,&dCoordinateX,&dCoordinateY,&nDemand,&nTimewindowE,&nTimewindowL,&dServicetime);
fprintf(stdout,"%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\n",num,dCoordinateX,dCoordinateY,nDemand,nTimewindowE,nTimewindowL,dServicetime);
}
fclose(fp);
system("pause");
return 0;
}
你那些读取都乱用的%d,%f,%lf要分清好不。全是数字要用什么数组呢
广告 您可能关注的内容 |