vc打开txt文件读取数据
#include<stdio.h>#include<stdlib.h>voidmain(){FILE*fp1;doublex1=0,y1=0,x1_x1=0,x1_y1=...
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp1;
double x1=0,y1=0,x1_x1=0,x1_y1=0;
int n;
int i;
printf("input n\n");
scanf("%d",&n);
struct point_type
{
double x;
double y;
double z;
};
struct point_type point[3000];
if((fp1=fopen("zhixian.txt","r"))==NULL)
{
printf("cant not open file");
exit(1);
}
for(i=0;i<n;i++)
{
fscanf(fp1,"%lf %lf",point[i],&point[i].x,&point[i].y);
x1+=point[i].x;
y1+=point[i].y;
x1_y1+=point[i].x*point[i].y;
x1_x1+=point[i].x*point[i].x;
}
fclose(fp1);
}
txt 1 1.001 0 2 2.001 0 3 2.998 0 4 3.999 0
为什么不能运行? 展开
#include <stdlib.h>
void main()
{
FILE *fp1;
double x1=0,y1=0,x1_x1=0,x1_y1=0;
int n;
int i;
printf("input n\n");
scanf("%d",&n);
struct point_type
{
double x;
double y;
double z;
};
struct point_type point[3000];
if((fp1=fopen("zhixian.txt","r"))==NULL)
{
printf("cant not open file");
exit(1);
}
for(i=0;i<n;i++)
{
fscanf(fp1,"%lf %lf",point[i],&point[i].x,&point[i].y);
x1+=point[i].x;
y1+=point[i].y;
x1_y1+=point[i].x*point[i].y;
x1_x1+=point[i].x*point[i].x;
}
fclose(fp1);
}
txt 1 1.001 0 2 2.001 0 3 2.998 0 4 3.999 0
为什么不能运行? 展开
1个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
struct point_type //结构体放外面
{
double x;
double y;
double z;
};
void main()
{
FILE *fp1;
double x1=0,y1=0,x1_x1=0,x1_y1=0;
int n;
int i;
printf("input n\n"); //输6
scanf("%d",&n);
struct point_type point[3000];
if((fp1=fopen("e:\\abc.txt","r"))==NULL) //这里是你的txt文件地址
{
printf("cant not open file");
exit(1);
}
for(i=0;i<n;i++)
{
fscanf(fp1,"%lf %lf",&point[i].x,&point[i].y);//这里不用加point[i]
x1+=point[i].x;
y1+=point[i].y;
x1_y1+=point[i].x*point[i].y;
x1_x1+=point[i].x*point[i].x;
printf("%lf %lf\n",point[i].x,point[i].y);
//不知道你要输出什么如果要控制台输出地话,就用printf,要写到另一个文件用fprintf
}
fclose(fp1);
}
这样你看一下,要什么具体要求就要你自己改了。
#include <stdlib.h>
struct point_type //结构体放外面
{
double x;
double y;
double z;
};
void main()
{
FILE *fp1;
double x1=0,y1=0,x1_x1=0,x1_y1=0;
int n;
int i;
printf("input n\n"); //输6
scanf("%d",&n);
struct point_type point[3000];
if((fp1=fopen("e:\\abc.txt","r"))==NULL) //这里是你的txt文件地址
{
printf("cant not open file");
exit(1);
}
for(i=0;i<n;i++)
{
fscanf(fp1,"%lf %lf",&point[i].x,&point[i].y);//这里不用加point[i]
x1+=point[i].x;
y1+=point[i].y;
x1_y1+=point[i].x*point[i].y;
x1_x1+=point[i].x*point[i].x;
printf("%lf %lf\n",point[i].x,point[i].y);
//不知道你要输出什么如果要控制台输出地话,就用printf,要写到另一个文件用fprintf
}
fclose(fp1);
}
这样你看一下,要什么具体要求就要你自己改了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询