简体   繁体   English

如果你不知道要读取的字符数,如何使用fgets?

[英]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): 您可以执行以下任何操作(并且可能还有其他一些我未提及的选项):

  1. Process the over-long lines in bits without assuming that there was a newline in between. 以位为单位处理过长的行而不假设其间存在换行符。
  2. Allocate memory for a longer line (say 8K to start with), copy the initial 4K into the allocated buffer, and read more data into the second half of the buffer, iterating until you find the end of line. 为更长的行分配内存(比如8K开始),将初始4K复制到分配的缓冲区,并将更多数据读入缓冲区的后半部分,迭代直到找到行尾。
  3. Use the POSIX 2008 function getline() which is available on Linux. 使用Linux上提供的POSIX 2008函数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.

相关问题 当我们不知道输入的数量时,如何在 C++ 中读取空格分隔的输入 - How to read space separated input in C++ When we don't know the Number of input 我不知道如何定位最高随机数的位置 - I don't know how to locate the position of the highest random number 不知道如何正确使用IShellWindows :: Item - don't know how to use IShellWindows::Item correctly 我不知道如何在 c++ 中使用 memcmp - I don't know how to use memcmp in c++ tinyxml2:如何知道从流缓冲区成功读取的字符数 - tinyxml2 : how to know number of characters read successfully from a stream buffer 如果您不知道要传递的方法的确切类,如何将方法传递给类 - How to pass a method to a class if you don't know the exact class of the method being passed 你应该如何初始化一个我不知道其内容的 Array 属性? - How are you supposed to initialize an Array attribute that I don't know the contents of? 如何读取文件中的字符数? - How to read the number of characters in a file? 我不知道如何获取从文件中读入的矩阵的const值 - I don't know how to get a const value for my matrix that is read in from a file 如果您在“我们不使用例外”阵营,那么您如何使用标准库? - If you're in the “we don't use exceptions” camp, then how do you use the standard library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM