关于读取txt文件的问题

bia天天pia 2014-04-02 11:26:47
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++;
}

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;
}
文件的第一排是513547.92 5403429.81 288.45 0
但调试发现结果 x y z为-6.2774385622041925e+066 i 为-842150451

希望大家帮忙看看~
...全文
141 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
我估计错了。参考下面:(没实际编译链接调试,不一定对,仅供参考)
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;
}
JiMoKuangXiangQu 2014-04-02
  • 打赏
  • 举报
回复
引用 楼主 u012793476 的回复:
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++; } 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; } 文件的第一排是513547.92 5403429.81 288.45 0 但调试发现结果 x y z为-6.2774385622041925e+066 i 为-842150451 希望大家帮忙看看~
LZ可参考如下方式读取。LZ代码失败的原因是前面的getline()已经读到了文件末尾,接下来的读取会失败。
#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
bia天天pia 2014-04-02
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
估计楼主犯了在调试时以为当前行已经执行过,其实还没有执行的低级错误!
我继续往下执行 i的值变了但是 x y z v都没有变
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
单步执行到这一行infile>>point[i].x>>point[i].y>>point[i].z>>point[i].v;时,还没执行对应功能,此时在调试器中观察对应变量的值是未初始化的随机值。
bia天天pia 2014-04-02
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
估计楼主犯了在调试时以为当前行已经执行过,其实还没有执行的低级错误!
没有太懂 能详细点么~
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
估计楼主犯了在调试时以为当前行已经执行过,其实还没有执行的低级错误!
JiMoKuangXiangQu 2014-04-02
  • 打赏
  • 举报
回复
另外,没必要还seekpos到开始位置。
JiMoKuangXiangQu 2014-04-02
  • 打赏
  • 举报
回复
引用 9 楼 u012793476 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] 我估计错了。参考下面:(没实际编译链接调试,不一定对,仅供参考)
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;
}
谢谢 还有两个问题 一个是使用seekpos是还得加什么头文件么? 另外一个是 我用double型 txt里例如513547.04就变成了513547.039999998 是什么原因还有如何解决呀[/quote] 至于这个,是浮点精度表示的问题,不是每个浮点数都能精确用计算机表示出来,因为数字是二进制表示的,能存储的数字个数是有限的,而数学上的浮点数是无限的,所以这样注定了有些浮点数不能被计算机的double,float表示。 如果想不损失精度,先用string读取进来,然后用高精度库如GMP处理。
JiMoKuangXiangQu 2014-04-02
  • 打赏
  • 举报
回复
引用 5 楼 JiMoKuangXiangQu 的回复:
[quote=引用 楼主 u012793476 的回复:] 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++; } 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; } 文件的第一排是513547.92 5403429.81 288.45 0 但调试发现结果 x y z为-6.2774385622041925e+066 i 为-842150451 希望大家帮忙看看~
LZ可参考如下方式读取。LZ代码失败的原因是前面的getline()已经读到了文件末尾,接下来的读取会失败。
#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
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
bia天天pia 2014-04-02
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
我估计错了。参考下面:(没实际编译链接调试,不一定对,仅供参考)
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;
}
谢谢 还有两个问题 一个是使用seekpos是还得加什么头文件么? 另外一个是 我用double型 txt里例如513547.04就变成了513547.039999998 是什么原因还有如何解决呀
赵4老师 2014-04-02
  • 打赏
  • 举报
回复
infile.open(dlg.m_path,ios::out); 成功了吗?
bia天天pia 2014-04-02
  • 打赏
  • 举报
回复
引用 5 楼 JiMoKuangXiangQu 的回复:
[quote=引用 楼主 u012793476 的回复:] 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++; } 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; } 文件的第一排是513547.92 5403429.81 288.45 0 但调试发现结果 x y z为-6.2774385622041925e+066 i 为-842150451 希望大家帮忙看看~
LZ可参考如下方式读取。LZ代码失败的原因是前面的getline()已经读到了文件末尾,接下来的读取会失败。
#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那段删除了也是读不出来 能帮我在我的基础上改成能得到 行数 并且读出来的代码么?

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧