使用 fstream 将文件读入单独的变量最有效
我有大量的文件,看起来有点像:
12-3-125-BINARYDATA
What would be the most effective way to save the 12
, 3
and 125
as separate integer变量,以及作为 char-vector
的 BINARYDATA
?
我真的很想使用 fstream
,但我不完全知道如何使用(让它与 std::strings
一起使用,但是 BINARYDATA部分都搞砸了)。
I have tons of files which look a little like:
12-3-125-BINARYDATA
What would be the most efficient way to save the 12
, 3
and 125
as separate integer variables, and the BINARYDATA
as a char-vector
?
I'd really like to use fstream
, but I don't exactly know how to (got it working with std::strings
, but the BINARYDATA
part was all messed up).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
读取数据最有效的方法是使用最少的 I/O 函数调用读取许多“块”或记录到内存中,然后解析内存中的数据。
例如,使用一次
fread
调用读取 5 条记录比调用 5 次fread
读取一条记录更有效。访问内存总是比访问文件等外部数据要快。某些平台具有内存映射文件的能力。这可能比使用 I/O 函数读取更有效。分析将确定最有效的。
固定长度记录总是比可变长度记录更有效。可变长度记录涉及读取直到读取固定大小或读取直到找到最终(哨兵)值。例如,文本行是一个变量记录,必须一次读取一个字节,直到找到终止行结束标记。在这种情况下,缓冲可能会有所帮助。
The most efficient method for reading data is to read many "chunks", or records into memory using the fewest I/O function calls, then parsing the data in memory.
For example, reading 5 records with one
fread
call is more efficient than 5 calls tofread
to read in a record. Accessing memory is always faster than accessing external data such as files.Some platforms have the ability to memory-map a file. This may be more efficient than reading the using I/O functions. Profiling will determine the most efficient.
Fixed length records are always more efficient than variable length records. Variable length records involve either reading until a fixed size is read or reading until a terminal (sentinel) value is found. For example, a text line is a variable record and must be read one byte at a time until the terminating End-Of-Line marker is found. Buffering may help in this case.
二进制数据是什么意思?它是 010101000 个字符还是“真正的”二进制数据?如果它们是真正的“二进制数据”,只需将文件作为二进制文件读取即可。首先读取第一个 int 的 2 个字节,接下来读取 1 个字节的 -,2 个字节的 3,依此类推,直到读取二进制数据的第一个 pos,只需获取文件长度并读取全部即可。
What do you mean by Binary Data? Is it a 010101000 char by char or "real" binary data? If they are real "binary data", just read the file as binary file. First read 2 bytes for the first int, next 1 bytes for -, 2 bytes for 3,and so on, until you read the first pos of binary data, just get file length and read all of it.