文件读取问题?

wenxiutot 2004-08-31 11:27:16
其中LIN 2是指线段的顶点数,下面是顶点坐标,
我要用这些点的坐标画直线,
LIN 2
PTS 40043.7691 70992.6427
PTS 40025.6408 71028.3792
EOL
LIN 2
PTS 40020.6998 71038.1196
PTS 39949.7098 71178.0632
EOL
我是新手见笑了谢谢了!
...全文
228 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
daylove 2004-09-19
  • 打赏
  • 举报
回复
不知道,学习一下
xhcumt 2004-09-19
  • 打赏
  • 举报
回复
应该还要知道文件头的信息吧,光知道这些应该是不行的。
shakaCY 2004-09-19
  • 打赏
  • 举报
回复
看看!
aychy 2004-09-06
  • 打赏
  • 举报
回复
补:test.txt文件内容
B 40043.7691 70992.6427
E 40025.6408 71028.3792
aychy 2004-09-06
  • 打赏
  • 举报
回复
我参照上面内容已经做出来了,但是用string代替char,编译没有通过,不知道怎么回事?c++ primer 上介绍文件操作的第二十章上,ifstream的参数明明用的是string。出错信息:e:\tmp\tmp4\tmp4.cpp(16) : error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char
,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
我用的是VC6.0,代码如下,
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;


int main()
{
//char a[]="test.txt";
std::string b="test.txt";
ifstream in(b);
char str;
char str1[100],str2[100];
while(!in.eof())
{
in >> str;
switch(str)
{
case 'B':
cout << "说明下一个数据是起始顶点数" <<endl;
in >> str1 >> str2;
cout << str1 << ' ' << str2 << endl;
break;
case 'E':
cout << "说明下一个数据是末尾顶点数" <<endl;
in >> str1 >> str2;
cout << str1 << ' ' << str2 << endl;
}
}
return 0;
}

请高手指导,谢谢!
roy 2004-09-03
  • 打赏
  • 举报
回复
vlong
kgha 2004-09-02
  • 打赏
  • 举报
回复
怎么才能把str[100]转化为整数?
hxblvc 2004-08-31
  • 打赏
  • 举报
回复
如:
我要读了下边这块数据. 如果.你的每个字符占1Byte.整数占4Byte.浮点数占8Byte
LIN 2
PTS 40043.7691 70992.6427
PTS 40025.6408 71028.3792
EOL
---------------------------
struct Point
{double x,double y};
char pC[3];
Point myP[2];
int t;

FILE *pf=fopen("**","rb");
fread(pC,1,3,pf);
if(pC is LIN)
{
fread(&t,1,4,pf);
if(t is 2)
{
fread()
...
}
}

就这样了.
hxblvc 2004-08-31
  • 打赏
  • 举报
回复
你的意思是不是说.这些数据保存在文件中的.你现在想把它们读到内存,然后再画出来.

如果是这个意的话.那么 你只提供这点信息是不行的.

要把这些数据正确的读到相应的变量.就行知道这些数据中每个元素在文件中所占的字节数.

不然没法读.我从从你上面给的数据中,无没看出它们所它的字节个数.

wenxiutot 2004-08-31
  • 打赏
  • 举报
回复
To hxblvc:我的顶点数目是不一定的,所以数据的字节数时没有规律的。

To qyshooter:我试了你这种方法出下面的错误,为什么呢?
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'char [100]' (or there is no acceptable conversion)


非完美主义者 2004-08-31
  • 打赏
  • 举报
回复
直接用ifstream类,比如:
ifstream in("filename");
char str[100];
while(!in.eof())
{
str<<in;
if (strcmp(str,"Lin"))
{
//说明下一个数据是顶点数
str<<in;
//此时把str转换成整数
}
if (strcmp(str,"Pts"))
{
//说明下一个数据是顶点坐标
str<<in;
//把str转换成整数
……
}
}
就这样。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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