C++ 读取文本文件问题

yanasdf789 yanasdf789 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想实现从文本文件中随机读取 几行的数据,
文本数据
882.840,55.244,7.261,68,69,64,-6.330
882.989,55.311,7.276,92,86,74,-4.290
882.877,55.349,7.266,79,77,65,-7.000
882.922,55.279,7.259,84,82,70,-15.130
882.884,55.314,7.264,82,80,68,-8.800
代码如下

FILE *file=fopen("D:\\tt.txt","r");
char str[100];
fgets(str,99,file);
fseek(file,0,0);
int lenth=strlen(str)*3; //获取每行的字节数*行数
fseek(file,lenth,0); //指针偏移量
Struct_PT temp1; //一个结构体
fscanf(file,"%f,%f,%f,%d,%d,%d,%f\n",&temp1.x,&temp1.y,&temp1.z,&temp1.r,&temp1.g,&temp1.b,&temp1.i);
float xx=temp1.x;
float yy=temp1.y;
float zz=temp1.z;
int rr=temp1.r;
int gg=temp1.g;
int bb=temp1.b;
float ii=temp1.i;

我读取第 一行 ,第二行 即 lenth=strlen(str)*2 是正确的
如果读取第三行开始lenth=strlen(str)*3 就错误了读不到数据
奇怪的问题?怎么结果啊
...全文
评论 7
收藏 已收藏
分享
返回顶部
yanasdf789
C++ 语言 65,214 成员 250,514 内容