文件数据的选择性读取

时间:2021-06-18 10:03:05
文件格式如下:
************************************************************************************
# oto project file generated by autopano

# input images:
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2586_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d0 e0 v50 a0 b0 c0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2587_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2588_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2589_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2590_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2591_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0
#-imgfile 993 660 "E:\task\autopano_v103模块\sample_pictures\DSC_2592_resize.JPG"
o f0 y+0.000000 r+0.000000 p+0.000000 u20 d=0 e=0 v=0 a=0 b=0 c=0

# Control points:

c n0 N1 x86.179192 y40.811302 X123.874603 Y476.327179
# Control Point No 0: 1.00000

c n0 N1 x61.899826 y59.407772 X97.157974 Y490.115082
# Control Point No 2: 1.00000

c n0 N1 x98.925049 y29.910719 X137.931442 Y466.834991
# Control Point No 4: 1.00000
************************************************************************************
如何从中绕过# Control points:以前的数据,直接读取控制点数据.存入struct point
{int num;
float x;
float y;
float X;
float Y;
};
但读入之前并不知有几个控制点,(以上例子是3个:NO 0,2,4)必须读到最后才知道几个点.
如:第一个点No1:x=86.179192;y=40.811302;X=123.874603;Y=476.327179;

6 个解决方案

#1


坐标的位数是固定的?

#2


坐标位数是固定的。有代码回复最好,谢谢

#3


不好意思,刚才有点事情耽搁了一下
#include <fstream>
#include <iostream>
#include <vector>
#include <memory>
#include <cstdlib>
using namespace std;

const size_t BUFSIZE = 1024;

struct Point
{
int num;
float x, y, X, Y;
};

void FindChar(const char* str, char chBeg, char chEnd, int& nOffset, size_t& len)
{
int i(0), j(0);
while(str[i] != chBeg)
{
++i;
}
j = ++i;

while(str[j] != chEnd)
{
++j;
}

nOffset = i;
len = j - i;
}

int main()
{
vector<Point> point;
Point ptTmp;
char chTmp[BUFSIZE];
char chFloat[BUFSIZE];
size_t unCount = 1;

memset(chTmp, '\0', BUFSIZE);
memset(chFloat, '\0', BUFSIZE);

ifstream input("D:\\Data.txt");
while(input.getline(chTmp, BUFSIZE))
{
if (chTmp[0] != 'c' && chTmp[2] != 'n')
{
if (chTmp[0] != '\0')
{
memset(chTmp, '\0', BUFSIZE);
}
continue;
}
else
{
int nOffset(0);
size_t nLen(0);

ptTmp.num = unCount++;

FindChar(chTmp, 'x', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.x = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'y', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'X', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.X = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'Y', '\n', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.Y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

point.push_back(ptTmp);
}
}
input.close();

vector<Point>::const_iterator iter = point.begin();
for ( ; iter != point.end(); ++ iter)
{
cout << "Num: " << (*iter).num << endl
 << "x: " << (*iter).x << endl
 << "y: " << (*iter).y << endl
 << "X: " << (*iter).X << endl
 << "Y: " << (*iter).Y << endl << endl;
}

return 0;
}

#4


为了避免精度缺失,LZ可以把Point结构体中的float换成double。

这个测试程序用cout显示出来的数据是截短了的,只是显示出来的结果不太准确而已,实际内存中的数值是比较准确的,如果把float换成double,内存中的数据就完全是文件中读出来的数值了。
如果楼主想在屏幕上显示完整的数据,请在cout中使用setprecision(10)。

#5


#include <fstream>
#include <iostream>
#include <vector>
#include <memory>
#include <cstdlib>
#include <iomanip>
using namespace std;

const size_t BUFSIZE = 512;

struct Point
{
int num;
double x, y, X, Y;
};

void FindChar(const char* str, char chBeg, char chEnd, int& nOffset, size_t& len)
{
int i(0), j(0);
while(str[i] != chBeg)
{
++i;
}
j = ++i;

while(str[j] != chEnd)
{
++j;
}

nOffset = i;
len = j - i;
}

int main()
{
vector<Point> point;
Point ptTmp;
char chTmp[BUFSIZE];
char chFloat[BUFSIZE];
size_t unCount = 1;

memset(chTmp, '\0', BUFSIZE);
memset(chFloat, '\0', BUFSIZE);

ifstream input("D:\\Data.txt");
while(input.getline(chTmp, BUFSIZE))
{
if (chTmp[0] != 'c' && chTmp[2] != 'n')
{
continue;
}
else
{
int nOffset(0);
size_t nLen(0);

ptTmp.num = unCount++;

FindChar(chTmp, 'x', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.x = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'y', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'X', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.X = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'Y', '\n', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.Y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

point.push_back(ptTmp);
}
}
input.close();

vector<Point>::const_iterator iter = point.begin();
for ( ; iter != point.end(); ++ iter)
{
cout << setprecision(10)
 << "Num: " << (*iter).num << endl
 << "x: " << (*iter).x << endl
 << "y: " << (*iter).y << endl
 << "X: " << (*iter).X << endl
 << "Y: " << (*iter).Y << endl << endl;
}

return 0;
}

#6


是的,不错,问题成功解决,谢谢了steven20027

#1


坐标的位数是固定的?

#2


坐标位数是固定的。有代码回复最好,谢谢

#3


不好意思,刚才有点事情耽搁了一下
#include <fstream>
#include <iostream>
#include <vector>
#include <memory>
#include <cstdlib>
using namespace std;

const size_t BUFSIZE = 1024;

struct Point
{
int num;
float x, y, X, Y;
};

void FindChar(const char* str, char chBeg, char chEnd, int& nOffset, size_t& len)
{
int i(0), j(0);
while(str[i] != chBeg)
{
++i;
}
j = ++i;

while(str[j] != chEnd)
{
++j;
}

nOffset = i;
len = j - i;
}

int main()
{
vector<Point> point;
Point ptTmp;
char chTmp[BUFSIZE];
char chFloat[BUFSIZE];
size_t unCount = 1;

memset(chTmp, '\0', BUFSIZE);
memset(chFloat, '\0', BUFSIZE);

ifstream input("D:\\Data.txt");
while(input.getline(chTmp, BUFSIZE))
{
if (chTmp[0] != 'c' && chTmp[2] != 'n')
{
if (chTmp[0] != '\0')
{
memset(chTmp, '\0', BUFSIZE);
}
continue;
}
else
{
int nOffset(0);
size_t nLen(0);

ptTmp.num = unCount++;

FindChar(chTmp, 'x', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.x = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'y', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'X', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.X = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'Y', '\n', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.Y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

point.push_back(ptTmp);
}
}
input.close();

vector<Point>::const_iterator iter = point.begin();
for ( ; iter != point.end(); ++ iter)
{
cout << "Num: " << (*iter).num << endl
 << "x: " << (*iter).x << endl
 << "y: " << (*iter).y << endl
 << "X: " << (*iter).X << endl
 << "Y: " << (*iter).Y << endl << endl;
}

return 0;
}

#4


为了避免精度缺失,LZ可以把Point结构体中的float换成double。

这个测试程序用cout显示出来的数据是截短了的,只是显示出来的结果不太准确而已,实际内存中的数值是比较准确的,如果把float换成double,内存中的数据就完全是文件中读出来的数值了。
如果楼主想在屏幕上显示完整的数据,请在cout中使用setprecision(10)。

#5


#include <fstream>
#include <iostream>
#include <vector>
#include <memory>
#include <cstdlib>
#include <iomanip>
using namespace std;

const size_t BUFSIZE = 512;

struct Point
{
int num;
double x, y, X, Y;
};

void FindChar(const char* str, char chBeg, char chEnd, int& nOffset, size_t& len)
{
int i(0), j(0);
while(str[i] != chBeg)
{
++i;
}
j = ++i;

while(str[j] != chEnd)
{
++j;
}

nOffset = i;
len = j - i;
}

int main()
{
vector<Point> point;
Point ptTmp;
char chTmp[BUFSIZE];
char chFloat[BUFSIZE];
size_t unCount = 1;

memset(chTmp, '\0', BUFSIZE);
memset(chFloat, '\0', BUFSIZE);

ifstream input("D:\\Data.txt");
while(input.getline(chTmp, BUFSIZE))
{
if (chTmp[0] != 'c' && chTmp[2] != 'n')
{
continue;
}
else
{
int nOffset(0);
size_t nLen(0);

ptTmp.num = unCount++;

FindChar(chTmp, 'x', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.x = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'y', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'X', ' ', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.X = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

FindChar(chTmp, 'Y', '\n', nOffset, nLen);
memcpy(chFloat, chTmp + nOffset, nLen);
ptTmp.Y = atof(chFloat);
memset(chFloat, '\0', BUFSIZE);

point.push_back(ptTmp);
}
}
input.close();

vector<Point>::const_iterator iter = point.begin();
for ( ; iter != point.end(); ++ iter)
{
cout << setprecision(10)
 << "Num: " << (*iter).num << endl
 << "x: " << (*iter).x << endl
 << "y: " << (*iter).y << endl
 << "X: " << (*iter).X << endl
 << "Y: " << (*iter).Y << endl << endl;
}

return 0;
}

#6


是的,不错,问题成功解决,谢谢了steven20027