简体   繁体   English

有效获取数据的算法

[英]Algorithm for taking data in efficient way

I have one binary file which I have created. 我已经创建了一个二进制文件。 In it, data is stored in binary form but I will show it in human readable form like ; 在其中,数据以二进制形式存储,但我将以人类可读的形式显示它,例如;

[someOtherData]opalgo$apollo$[someOtherData]
               ^
               ^
               ^
             file pointer

I want hold that data "opalgo" in temp_S, which is declared as string.To note, not shown at example, I don't know what data is this place and its length. 我想将数据“ opalgo”保存在temp_S中,该数据声明为string。要注意,在示例中未显示,我不知道该位置是什么数据及其长度。

To take data reside in binary file ; 将数据存放在二进制文件中;

  • First, I have calculated its length by incrementing file pointer and each time, I did comparison with '$'. 首先,我通过增加文件指针来计算其长度,并且每次都与“ $”进行比较。
  • Second, I come back by length of data, for this case |opalgo| 其次,我根据数据长度返回,在这种情况下| opalgo | = 6. = 6。
  • Third, each time, I take one char and concatenate it with string, which is initialized with "" . 第三,每次使用一个char并将其与字符串连接,该字符串以“”初始化。 that is, string += temp_Char ; 也就是说,字符串+ = temp_Char;

Above algorithm is mess, of course in my opinion. 在我看来,以上算法是一团糟。 I couldn't find efficient way to take data from binary file. 我找不到从二进制文件中获取数据的有效方法。 Now, I am a bit ashamed. 现在,我有点as愧。 Can you give me a better way ? 你能给我一个更好的方法吗?

EDIT: I don't know file size, but as you know, I can calculate by using seekg and tellg. 编辑:我不知道文件大小,但是如您所知,我可以使用seekg和tellg进行计算。

Instead of doing it in three steps, it could all be done in the first: While looking for ther terminating '$' just put the characters into temp_S . 而不是分三步来完成,而是可以在第一步中全部完成:寻找终止符'$'时,只需将字符放入temp_S No need to calculate length. 无需计算长度。

std::ifstream input(...);

std::string temp_S;
char c;
while (input.get(c))
{
    if (c == '$')
        break;  // Found terminator, read no more into string
    temp_S += c;
}

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

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