基础知识(十)C++文件读写.txt

一、写入文件
	ofstream f1("index.txt");//创建文件,并写入
	f1<<"姓名:"<<"hjimce"<<endl;
	f1<<"家庭地址:"<<"福建厦门"<<endl;
	f1.close();

二、读取文件

#include <iostream>
#include <fstream> //文件输入输出流
#include <sstream>
vector<vec2> CFeatureReuseDoc::read_mesh2d()
{
	vector<vec2>mesh2dvs;
	using std::getline;

	using std::string;

	std::ifstream file("example.txt");

	string line;
	int ibugId = 1;
	while (getline(file, line))
	{
		std::stringstream lineStream(line);
		vec2 landmarkA;
		vec2 landmarkB;
		vec2 landmarkC;
		if (!(lineStream>>landmarkA[0] >> landmarkA[1]>> landmarkB[0] >> landmarkB[1]>> landmarkC[0] >> landmarkC[1])) {
			throw std::runtime_error(string("Landmark format error while parsing the line: " + line));
		}

		mesh2dvs.push_back(landmarkA);
		mesh2dvs.push_back(landmarkB);
		mesh2dvs.push_back(landmarkC);
		++ibugId;
	}
	return mesh2dvs;
}


你可能感兴趣的:(基础知识(十)C++文件读写.txt)