C++ 从文件中读取double类型数据

来源:互联网 发布:java 双亲委派模型 编辑:程序博客网 时间:2024/06/04 10:39

直接贴代码吧,一看就懂

#include <iostream>#include <cmath>#include <iomanip>#include <fstream>using namespace std;/*从文件中读取数据,并计算相邻两点的距离上下两行的数据默认是两个相邻点的数据*/char buffer[250];int main(){    double dist;    double x[100],y[100];    int tot=1;    ifstream infile("data.txt");    ofstream outfile("result.txt");    while (!infile.eof()){        infile.getline(buffer,20); //整行读入        sscanf(buffer,"%lf %lf",&x[tot],&y[tot]);        tot++;    }    tot--;    for(int i=1;i<tot;i++) {        dist=sqrt(pow(x[i]-x[i+1],2)+pow(y[i]-y[i+1],2));        outfile<<dist<<endl;    }    infile.close();    outfile.close();    return 0;}



原创粉丝点击