[英]How to use fgets if you don't know the number of characters to be read?
I need to read a file and send the text from it to a string so I can parse it. 我需要读取一个文件并将其中的文本发送到一个字符串,以便我可以解析它。 However, the program won't know exactly how long the file is, so what would I do if I wanted to use
fgets()
, or is there a better alternative? 但是,程序不会确切地知道文件的长度,所以如果我想使用
fgets()
,或者有更好的选择,我该怎么办?
Note: 注意:
char *fgets(char *str, size_t num, FILE *stream);
Don't forget that fgets()
reads a line at a time, subject to having enough space. 不要忘记
fgets()
读取一行,但要有足够的空间。
Humans seldom write lines longer than ... 80, 256, pick a number ... characters. 人类很少写行超过... 80,256,选择一个数字......字符。 POSIX suggests a line length of 4096. So, I usually use:
POSIX建议行长度为4096.所以,我通常使用:
char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp))
{
...process line...
}
If you are worried that someone might provide more than 4K of data in a single line (and a machine generated file, such as HTML or JSON, might contain that), then you have to decide what to do next. 如果您担心某人可能在一行中提供超过4K的数据(并且机器生成的文件,例如HTML或JSON,可能包含该数据),那么您必须决定下一步该做什么。 You can do any of the following (and there are likely some other options I've not mentioned):
您可以执行以下任何操作(并且可能还有其他一些我未提及的选项):
getline()
which is available on Linux. getline()
。 It does memory allocation for you. You can use fgets iteratively, but a simpler alternative is (stdio.h's) getline . 你可以迭代地使用fgets,但更简单的替代方法是(stdio.h) getline 。 It's in POSIX, but it's not standard C.
它在POSIX中,但它不是标准C.
Since you're using C++ though, can you use std::string functions like iostream's getline ? 既然你正在使用C ++,你可以使用像iostream的getline这样的std :: string函数吗?
If you're not on a POSIX system and don't have getline
available, take a look at Chuck Falconer's public domain ggets
/ fggets
functions which dynamically grow a buffer to consume an entire line. 如果您不在POSIX系统上并且没有
getline
可用,请查看Chuck Falconer的公共域ggets
/ fggets
函数 ,这些函数动态增加缓冲区以消耗整行。 (That link seems to be down right now, but archive.org has a copy .) (这个链接现在似乎已经关闭,但archive.org有一份副本 。)
Allocate a buffer (the one that str
points to), and pass the size of the buffer for num
. 分配缓冲区(
str
指向的缓冲区),并将缓冲区的大小传递给num
。 The actual space taken up will only be the length of the text read by fgets
. 占用的实际空间仅为
fgets
读取的文本的长度。
Something like: 就像是:
char str[1000];
fgets(str, 1000, &file);
If the next line only has 10 characters before the newline, then str will hold those 10 characters, the newline, and the null terminator. 如果下一行在换行符之前只有10个字符,则str将保存这10个字符,换行符和空终止符。
Edit : just in case there is any confusion, I didn't intend the above to sound as if the extra space in the buffer isn't in use. 编辑 :以防万一有任何混淆,我不打算上面的声音,好像缓冲区中的额外空间没有被使用。 I only meant to illustrate that you don't need to know ahead of time how long your string is going to be, as long as you can put a maximum length on it.
我只是想说明你不需要提前知道你的字符串将持续多长时间,只要你可以在它上面放一个最大长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.