简体   繁体   English

在C中寻求档案的简单问题

[英]Simple question on file seeking in C

Say I write in a file 说我写一个文件

Mesh: 1
    Vertices: 345
    Indices: 123
    V: 1,3,4 1,4,5 ..
Mesh: 2
    Vertices: 456
    Indices: 42
etc.

How do I go about seeking at any position? 我该如何寻找任何位置? Eg I want to go to Vertices: of Mesh 2 or V: of Mesh 3 etc. 例如,我想转到网格2的顶点:或网格3的V:等等。

What's the proper way to go about these things? 处理这些事情的正确方法是什么?

You would normally use a binary format. 通常,您将使用二进制格式。 One way would be to allocate a certain amount of space as a header in the file. 一种方法是在文件中分配一定数量的空间作为标题。 Here, you put the mesh numbers, vertex and index counts, and an offset into the file where the vertex data begins. 在这里,您将网格编号,顶点和索引计数以及偏移量放入顶点数据开始的文件中。 You read the header when loading the file, then seek to the appropriate place to read the data you want. 加载文件时,您先阅读了标头,然后搜索到适当的位置以读取所需的数据。

There is no efficient way of random seeking in text file formats. 在文本文件格式中没有有效的随机查找方法。 This is because you cannot know the right offset in the file without reading all the contents before. 这是因为您必须先读取所有内容才能知道文件中的正确偏移量。 The only way of processing these is sequential - from beginning to end. 处理这些错误的唯一方法是顺序的-从头到尾。

So read and parse the entire file into some data structure in memory. 因此,将整个文件读取并解析为内存中的某些数据结构。 Then use this structure instead of the file as needed. 然后,根据需要使用此结构而不是文件。

If the file is too large to keep everything in memory (these days it's highly improbable), read through the file without storing everything in memory - instead store just file offsets to the beginning of each Mesh in an array. 如果文件太大而无法将所有内容保留在内存中(这几天来极不可能),请在不将所有内容存储在内存中的情况下通读文件-而是仅将文件偏移量存储到数组中每个Mesh的开头。 Then you can easily seek to the right place. 然后,您可以轻松地找到正确的地方。

Open the file for reading, and read a line until End-of-File (EOF) is reache. 打开文件进行读取,然后读取一行直到到达文件末尾(EOF)。 For each read line, check if the line matches with your query. 对于每个读取的行,请检查该行是否与您的查询匹配。 If match, report and return. 如果匹配,报告并返回。 Otherwise, move on to the next line. 否则,请移至下一行。

The main cognitive work is checking for matches. 主要的认知工作是检查比赛。 Have a well defined format and easily parse-able format for the lines to make your job easy. 行的格式明确且易于解析,以简化您的工作。

As other answers have pointed out, C can only seek to byte offsets within files. 正如其他答案所指出的那样,C只能在文件内寻求字节偏移量。

However, if your "Mesh" objects are always stored in the file in numerical order, then you do not have to read the entire file sequentially to find the Mesh that you are after. 但是,如果“网格”对象始终按数字顺序存储在文件中,则不必顺序读取整个文件来查找要查找的网格。 You can instead perform a binary search on the file - whenever you seek to a position in the file, scan ahead to find the next Mesh . 您可以改为对文件执行二进制搜索-每当您寻找文件中的某个位置时,请向前扫描以查找下一个Mesh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM