加上头文件
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;//命名空间也要加
1,按行读文件
//读txt文件
ifstream infile;//定义文件变量
infile.open(GeoXYPath,ios::in);//打开txt
if(!infile)
{
AfxMessageBox("读取txt文件失败!");
return ;
}
string temp,temp2mat;
float pixelTemp[6];//此处只读6个数
int i = 0;
while(getline(infile,temp)) //读取一行,直到所有行读完
{
istringstream LineBand(temp);//转化成数据流
while(LineBand>>temp2mat) //每一行以空格为间隔读数,该行读完则跳出循环
{
pixelTemp[i] = atof(temp2mat.data());//字符转化成整型
i++;
}
}
infile.close();
2.按行写文件,每次写都接着上次的文件尾写
ofstream outfile;
//ios::app指从文件尾开始输出
outfile.open(OutPutPath,ios::out|ios::app);
if(!outfile)
{
AfxMessageBox("创建txt文件失败!");
return ;
}
//使输出格式为浮点
outfile<<fixed<<Y<<" "<<X; //以空格为间隔写 x y
outfile<<"\n"; //一行写完换行
outfile.close();