读取BSDF数据格式
我被要求编写一个读取 Zemax定义的BSDF数据格式 此类文件的示例可在以下页面找到:BSDF 文件示例
如果可能的话,我想仅使用标准 ifstream
函数。
我已经在一个专门的类中准备好了所有必要的数据成员。
我现在正在尝试编写从文件中读取数据的函数。
问题:
如何排除注释行?如文档所述,它们以哈希
#
开头,我想要类似void ReadBSDFFile(myclass &object) { ifstream 输入文件; infile.open(对象.BRDF文件名); 字符c; infile.get(c); while (c == "#") // 问题,显然我不能用这种方式进行比较。我该怎么做呢? { getline(infile, 行); infile.get(c); } // 此时我想返回一个字符(因为我不想丢失以 *c* 结尾的非哈希字符) infile.seekg(-1, ios_base::cur); // 完成剩下的所有工作 infile.close(); }
- 以类似的方式获取类似
的内容,我想稍后验证我是否处于正确的行(例如“AngleOfIncidence”)线)。我可以用这种方法吗?
字符串 AngleInc; 输入文件>>角度公司; if (AngleInc != "AngleOfIncidence") { //错误 }
感谢任何评论/帮助的人。欢迎提出建设性批评。
Federico
编辑:
感谢下面的 Joachim Pileborg,我成功地继续到文件的数据块部分。
现在我有以下问题。当到达数据块时,我编写了以下代码,但在第二次迭代 (i = 1
) 时,我收到了 TIS 行的错误消息。 有人可以帮助我理解为什么这不起作用吗? 谢谢
注意:blocks
是 AngleOfIncidence 线上的数字,rows
是 ScatterAzimuth 线上的数字,columns
是 ScatterRadial 上的数字。我测试并验证了这部分功能按预期工作。
// now reading the data blocks.
for (int i=0; i<blocks; i++)
{
// TIS line
getline(infile, line);
if (line.find("TIS") == string::npos)
{
// if not, error message
}
// Data block
for (int j=0; j<rows; j++)
{
for (int k=0; k<columns; k++)
{
infile >> object.BRDFData[i][j][k];
}
}
}
编辑2:
解决了添加infile.seekg(+2, ios_base::cur);
作为i
循环的最后一行的问题。
I have been required to write a function that reads the BSDF data format defined by Zemax
An example of such file can be found at the following page: BSDF file example
I would like to use, if possible, only standard ifstream
functions.
I have already prepared all the necessary datamembers inside a dedicated class.
I am now trying to write the function that reads the data from the file.
Problems:
how do I exclude comment lines? as documented, they start with an hash
#
I was going for something likevoid ReadBSDFFile(myclass &object) { ifstream infile; infile.open(object.BRDFfilename); char c; infile.get(c); while (c == "#") // Problem, apparently I cannot compare in this way. How should I do it? { getline(infile, line); infile.get(c); } // at this point I would like to go back one character (because I do not want to lose the non-hash character that ended up in *c*) infile.seekg(-1, ios_base::cur); // Do all the rest infile.close(); }
in a similar way, I would like to verify that I am at the correct line later on (e.g. the "AngleOfIncidence" line). Could I do it in this way?
string AngleInc; infile >> AngleInc; if (AngleInc != "AngleOfIncidence") { //error }
Thanks to anyone who will comment/help. Constructive criticism is welcomed.
Federico
EDIT:
Thanks to Joachim Pileborg below, I managed to proceed up to the data blocks part of the file.
Now I have the following problem. When reaching the datablocks, I wrote the following piece of code, but at the second iteration (i = 1
) i receive the error message for the TIS line.
Could someone help me understand why this does not work?
Thanks
Note: blocks
is the number on the AngleOfIncidence line, rows
the one on the ScatterAzimuth line and columns
the one on the ScatterRadial. I tested and verified that this part of the function works as desired.
// now reading the data blocks.
for (int i=0; i<blocks; i++)
{
// TIS line
getline(infile, line);
if (line.find("TIS") == string::npos)
{
// if not, error message
}
// Data block
for (int j=0; j<rows; j++)
{
for (int k=0; k<columns; k++)
{
infile >> object.BRDFData[i][j][k];
}
}
}
EDIT 2:
solved adding infile.seekg(+2, ios_base::cur);
as a last line of the i
loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读循环可以像这样简化:
它可能不是最佳的,只是写在我脑海中的东西,但应该有效。
The reading loop could be simplified like this:
It's might not be optimal, just something written at the top of my head, but should work.
根据您提供的格式描述:
所以你需要做的是以下
你使用的
while
是错误的。使用getLine
函数代替并比较它的第一个带有#
的字符。From the description of the format you provided:
So what you need to do is the following
The
while
you have used is wrong. Use thegetLine
function instead and compare its first character with the#
.