ofstream o_file1, o_file2; 为什么第一个写文件里面什么都没有,第二个成功?
弄的一个写文件:
- C/C++ code
ofstream o_file1, o_file2; o_file1.open("LiDAR1.txt",ios::trunc); o_file2.open("LiDAR2.txt",ios::trunc); .................. int i = 0; while (reader.ReadNextPoint()) { // Get a point liblas::Point const& point = reader.GetPoint(); if (DataID == 1) { pVertices[i].x = point.GetX() - 277085; pVertices[i].y = point.GetY() - 4180763; pVertices[i].z = point.GetZ() - 2464; pVertices[i].CVector3_DataID = DataID; o_file1<<pVertices[i].x<<" "<<pVertices[i].y<<" "<<pVertices[i].z<<"\n"; } else { pVertices[i].x = point.GetX() - 277085; pVertices[i].y = point.GetY() - 4180763+0.364; pVertices[i].z = point.GetZ() - 2464; pVertices[i].CVector3_DataID = DataID; o_file2<<pVertices[i].x<<" "<<pVertices[i].y<<" "<<pVertices[i].z<<"\n"; } i++; } o_file1.close(); o_file2.close(); 第二个o_file2成功写文件。第一个o_file1只是创建了txt文件,但是一代开,里面什么都没有
是不是o_file1需要写的数据太多所以就干脆空白?(但是也不是太多数据,最多是o_file2的两倍)
[解决办法]
if (DataID == 1)的大括号里设个断点,看看到底有没有走进去
就看到的这段代码,DataID从头到尾没变化的,ifelse分支只可能走一条
[解决办法]
不要用ofstream,效率低,问题多,直接用fopen()\fwrite()\fread()\fclose();