使用C/C++读取文件格式以适当的方式
您将如何使用 C 或 C++ 读取简单的文件格式?
例如,采用 Wavefront .obj 文件格式,示例:
# this is a box
o 1
v -0.5 -0.5 0.5
v -0.5 -0.5 -0.5
v -0.5 0.5 -0.5
v -0.5 0.5 0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
v 0.5 0.5 -0.5
v 0.5 0.5 0.5
usemtl Default
f 4 3 2 1
f 2 6 5 1
f 3 7 6 2
f 8 7 3 4
f 5 8 4 1
f 6 7 8 5
由于文件可能非常大,因此我使用运算符 [] 创建了一个中间类 (FileBuffer)。它在内存中只有 4096 字节的文件,并在需要时读取新的部分。 文件格式非常简单,但我不喜欢使用像 flex/bison 这样的东西。这只会让事情变得复杂。
解释这种(类型)文件的正确方法是什么?目前我有很多嵌套的 for/while 循环和许多跟踪计数器。还有许多 switch/elseif 语句。我如何使这段代码可维护并且整体上更加结构化?
谢谢。
How would you go about reading a simple file format using C or C++?
For example, take the Wavefront .obj file format, sample:
# this is a box
o 1
v -0.5 -0.5 0.5
v -0.5 -0.5 -0.5
v -0.5 0.5 -0.5
v -0.5 0.5 0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
v 0.5 0.5 -0.5
v 0.5 0.5 0.5
usemtl Default
f 4 3 2 1
f 2 6 5 1
f 3 7 6 2
f 8 7 3 4
f 5 8 4 1
f 6 7 8 5
Since the files can be quite large, I made an intermediate class (FileBuffer) with an operator[]. It only every has 4096 bytes of the file in memory, and reads new parts whenever needed.
The file format is really simple, but I don't prefer to use something like flex/bison for this. It would only complicate matters.
What would be a proper way to go about interpreting this (kind of) file? Currently I have a whole lot of nested for/while loops and many counters keeping track. Also many switch/elseif statements. How would I make this code maintainable and more structured overall?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果是我,我会尽可能多地利用标准库:
If it were me, I'd leverage as much of the standard library as I could:
如果我正确理解格式,每一行都定义。
特定类型的数据,其类型由第一个确定
单词。我首先定义一个抽象基类,然后
每种类型线的类的具体实例; ID
在 std::map
为了读取文件,我可能会安装一个过滤streambuf
删除上游的注释和空行。然后
一个简单的循环就可以解决问题:
If I understand the format correctly, each line defines
a specific type of data, with the type determined by the first
word. I'd start by defining an abstract base class and
a concrete instance of the class for each type of line; I'd
register these instances in a
std::map<std::string,
.LineParser*>
For reading the file, I'd probably install a filtering streambuf
to get rid of the comments and the empty lines upstream. Then
a simple loop would do the trick:
我将从定义(或获取)文件语法/结构开始。
然后根据该语法为输入流构建一个解析器。
I would start with defining (or obtaining) the file grammar/structure.
Then build a parser for the input stream based on that grammar.