Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fgets if you don't know the number of characters to be read?

Tags:

c++

c

fgets

fstream

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?

Note:

char *fgets(char *str, size_t num, FILE *stream);
like image 359
vette982 Avatar asked May 21 '10 04:05

vette982


People also ask

Can fgets read numbers?

It's able to recognize abc and a12 as invalid numbers and so they are discarded.

Does fgets include null character?

The fgets() function stores the result in string and adds a null character (\0) to the end of the string. The string includes the new-line character, if read.

How does fgets () read the data from a file?

The fgets() function reads characters from the current stream position up to and including the first new-line character (\n), up to the end of the stream, or until the number of characters read is equal to n -1, whichever comes first.

How do I read a line in fgets?

The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.


3 Answers

Don't forget that fgets() reads a line at a time, subject to having enough space.

Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I usually use:

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. 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.
  3. Use the POSIX 2008 function getline() which is available on Linux. It does memory allocation for you.
like image 154
Jonathan Leffler Avatar answered Oct 04 '22 17:10

Jonathan Leffler


You can use fgets iteratively, but a simpler alternative is (stdio.h's) getline. It's in POSIX, but it's not standard C.

Since you're using C++ though, can you use std::string functions like iostream's getline?

like image 32
Matthew Flaschen Avatar answered Oct 04 '22 17:10

Matthew Flaschen


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. (That link seems to be down right now, but archive.org has a copy.)

like image 34
jamesdlin Avatar answered Oct 04 '22 16:10

jamesdlin