C++_读写TXT文件

将信息写入txt文件

string filename = "points_txt/frame_" + to_string(i + start_idx) + ".txt";
ofstream fout(filename.c_str(), ios::trunc); // 将点的信息写入文件, 先将文件清空
if (!fout) {
	cout << "文件打开失败" << endl;
}
// 获取当前帧的位置和颜色
ProcessPoints pps0 = frames_pps[i];
vector pos0 = pps0.frame_pos(); 
vector color0 = pps0.frame_color(); 
for (int j = 0; j < pos0.size(); ++j) {
	glm::vec3 p = pos0[j], c = color0[j];
	fout << p[0] << " " << p[1] << " " << p[2] << " " << c[0] << " " << c[1] << " " << c[2] <

将信息从txt文件中读出

    // 从文件中读出点的信息
	string filename = "points_txt/frame_0.txt";
	ifstream in_file;
	in_file.open(filename);
	if (!in_file.is_open()) {
		cout << "ERROR::FILE_NOT_SUCCESSFULLY_OPEN: " << filename << endl;
	}
	unsigned int amount = 1375009;
	glm::mat4* modelMatrices = new glm::mat4[amount];
	glm::vec3* colors = new glm::vec3[amount];
	char ds[100];
	//cout << ds << endl;
	int line = 0;
	while (line < amount) {
		in_file.getline(ds, 100);
		glm::vec3 pos, color;
		sscanf_s(ds, "%f %f %f %f %f %f", &(pos[0]), &(pos[1]), &(pos[2]), &(color[0]), &(color[1]), &(color[2]));
		if (line == 1375008) {
			cout << pos[0] << " " << color[0] << endl;
		}
		line++;
	}

 

你可能感兴趣的:(C++)