从二进制文件动态大小的fread串 [英] fread string of dynamic size from a binary file

查看:157
本文介绍了从二进制文件动态大小的fread串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何从一个二进制文件使用的fread()读取一个字符串。

I need to know how to read a string from a binary file using fread ().

事情是,据我了解,如果我想一些字符串的值复制到宣布这样一个新的字符串:

Thing is, as I understand, if I want to copy the value of some string to a new string declared like this:

char *string;

我需要使用首先计算其他字符串的长度的的strlen()的,使用该值保留内存为我的新字符串的的malloc 的,然后再复制值为0,用另一串到我的新字符串的的strcpy()

I need to calculate first the length of the other string using strlen (), use that value to reserve memory for my new string with malloc, and then copy the value o the other string to my new string using strcpy ()

有点像这样的:

newLength = strlen ( otherString ) + 1;
string = malloc ( sizeof ( char ) * newLength );
if  ( string == NULL ) {
    return ( FALSE )
}

但是,如果我从一个二进制文件中读取数据,我试图从该文件中读取一个字符串,但我不知道事前它的长度,所以我不能使用malloc保留内存为字符串说会发生什么?

But what happens if I am reading data from a binary file, and I am trying to read a string from said file but I don't know beforehand its length, therefore I can't use malloc to reserve memory for said string?

将这工作,即使我没有预留了字符串内存,但(我不那么信服):

Would this work, even though I haven't reserved memory for the string yet (I am not so convinced)?:

fread ( string, sizeof ( char ), strlen ( string ), currentFile );

我现在有点卡住。希望你们可以摆脱我一些启发,指导我一下。

I am kinda stuck now. Hopefully you guys can shed me some light and guide me a bit.

推荐答案

您的问题具有一定的混合内容。你说'二进制'文件,但你想从中读取字符串数据。从文件中解析字符串,通常意味着该文件在本质上是文本。然而,在不知道的先验的,你正在阅读的字符串的长度,你可以读取该文件逐字节,字节计数,直到你达到一个字符串结束,之后你分配一致缓冲区,快退的文件,并阅读到您的缓冲区。

Your question has some mixed content. You say 'binary' file, and yet you want to read string data from it. Parsing strings from a file generally implies that the file is textual in nature. Nevertheless, without knowing a priori, the length of the string you're reading, you can read the file byte-by-byte, counting bytes until you reach a string terminator, after which you allocate the accordant buffer, rewind the file, and read into your buffer.

另外,你可以pre-分配一个任意大的缓冲区,而不用担心未使用的金额。如果你需要阅读很多不同的金额,由你包裹出最佳读取每个字符串pre-分配内存,可能使用的的realloc()的,如果你用尽缓冲,你可以更节俭。

Alternatively, you can pre-allocate an arbitrarily large buffer, and not worry about the unused amounts. You could be more thrifty if you need to read many different amounts, by pre-allocating memory that you parcel out optimally to read each string, possibly using realloc() if you exhaust the buffer.

FREAD()的没有做字符串面向读,在意义上,说的的fscanf()的,这将空 - 终止与%s的扫描字符串格式说明。的 FREAD()的是与数据无关,根本不是填充指定的缓冲区,还是不行,如果档案结尾。如果二进制的数据包含null终结,也许这就是你要使用的东西,但它是值得重新思考,在我看来。

fread() does not do a string oriented read, in the sense of, say fscanf(), which will null-terminate a string scanned with the %s format specifier. fread() is data-agnostic, and simply either fills the specified buffer, or not, if end-of-file. If the 'binary' data contains null terminators, perhaps that is what you want to use, but it's worth a re-think, in my opinion.

如果你打算使用动态内存分配的字符串指针您的声明只为宜。如果你想使用定义你的字符串分配存储空间,则必须将其定义为一个数组类型。

Your declaration is only appropriate if you intend to use the string pointer for dynamic memory allocation. If you want to allocate storage for your string using the definition, you must define it as an array type.

char string[1000];

这篇关于从二进制文件动态大小的fread串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆