65,187
社区成员




void CMy2011302580040View::OnFilter()
{
// TODO: Add your command handler code here
FilDlg dlg;
if(dlg.DoModal() == IDOK)
{
string str1;
ifstream infile;
infile.open(dlg.m_path,ios::out);
int count=0;
while(getline(infile,str1))
{
count++;
}
//此时文件指针已经移到文件末尾了,下面想从头读取文件数据必须加一句:
infile.seekpos(0);
struct imagepoint
{
double x,y,z;
int v;
};
imagepoint* point=new imagepoint[count];
for(int i=0;i<count;i++)
{
infile>>point[i].x>>point[i].y>>point[i].z>>point[i].v;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("Input.txt", ios::out);
{
struct imagepoint
{
double x, y, z;
int v;
};
imagepoint point[2];
for (int i=0; i < 2; ++i) {
infile >> point[i].x >> point[i].y >> point[i].z >> point[i].v;
cout << point[i].x << " " << point[i].y << " "
<< point[i].z << " " << point[i].v << endl;
}
}
infile.close();
cin.get();
return 0;
}
Input(Input.txt):
513547.92 5403429.81 288.45 0
3547.92 54429.81 28.45 6
Output(Console):
513548 5.40343e+006 288.45 0
3547.92 54429.8 28.45 6
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("Input.txt", ios::out);
{
struct imagepoint
{
double x, y, z;
int v;
};
imagepoint point[2];
for (int i=0; i < 2; ++i) {
infile >> point[i].x >> point[i].y >> point[i].z >> point[i].v;
cout << point[i].x << " " << point[i].y << " "
<< point[i].z << " " << point[i].v << endl;
}
}
infile.close();
cin.get();
return 0;
}
Input(Input.txt):
513547.92 5403429.81 288.45 0
3547.92 54429.81 28.45 6
Output(Console):
513548 5.40343e+006 288.45 0
3547.92 54429.8 28.45 6
[/quote]
应该不至于,应该的是调试的时候超出那个{}作用域看不到数据信息了吧。
修改后的代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
struct imagepoint
{
double x, y, z;
int v;
};
int main()
{
ifstream infile;
infile.open("Input.txt", ios::out);
vector<imagepoint> points;
string line;
int count = 0;
while (getline(infile, line)) {
count++;
istringstream iss(line);
imagepoint pt;
iss >> pt.x >> pt.y >> pt.z >> pt.v;
points.push_back(pt);
//cout << pt.x << ' ' << pt.y << ' ' << pt.z << ' ' << pt.v << endl;
}
infile.close();
return 0;
}
Input(Input.txt):
513547.92 5403429.81 288.45 0
3547.92 54429.81 28.45 6
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("Input.txt", ios::out);
{
struct imagepoint
{
double x, y, z;
int v;
};
imagepoint point[2];
for (int i=0; i < 2; ++i) {
infile >> point[i].x >> point[i].y >> point[i].z >> point[i].v;
cout << point[i].x << " " << point[i].y << " "
<< point[i].z << " " << point[i].v << endl;
}
}
infile.close();
cin.get();
return 0;
}
Input(Input.txt):
513547.92 5403429.81 288.45 0
3547.92 54429.81 28.45 6
Output(Console):
513548 5.40343e+006 288.45 0
3547.92 54429.8 28.45 6
[/quote]
谢谢 但是我为什么把count那段删除了也是读不出来 能帮我在我的基础上改成能得到 行数 并且读出来的代码么?