Fgets

Anything related to C or C++ standard library functions `fgets` (C) or `std::fgets` (C++)

Getline

A C function that reads an entire line from a stream



Edit comments safer

Example

"If you can bound the maximum length of a line fgets may be a better way to read each line;but since you mention c++ you might consider using instead getline caveat fgets also put the n in the buffer it fills getline doesn t"

from question  

Outputting from file one line at a time

"One more edit if you want to use getline instead which you asked about in the comments - and it s even safer than fgets since it will increase the buffer size as needed you would change to change the code a little bit"

from question  

How to use arbitrary number of scanf() format specifiers?

Others

Example

If you use fgets which is fine you would have to add code to check for a short or incomplete read of each line and realloc and strcat the line until a complete read takes place;getline just makes life simpler here

from question  

How to copy from a file to an array

Fgets is the most convenient standard library function for reading files one line at a time;gnu getline is even more convenient but it is non-standard

from question  

How to read one whole line from text file using <

If you cannot tolerate that use getline it s harder to use so use fgets if in doubt

from question  

Inputting an arithmatic statement in c and return the value

You should reformat your code to be in the style i also recommend using fgets or getline for reading input just in case you decide to put in 100 characters when you only set aside space for 20

from question  

Problems with STRCPY and STRCMP when finding the smallest and largest problems

As proposed by fluter fgets will allow you read the file line by line;getline is another better imho solution it s manpage provides a simple usage example

from question  

C programming (reading information from file to variables)

Since i provided a getline example above your read loop can be much better written with fgets as follows

from question  

Error using malloc

Yes it s possible to use fgets and fputs if you re using posix systems you might find getline better than fgets but you d still use fputs with d

from question  

Copying files using fgets instead of fgetc?

Getline is far more flexible handling the allocation of space for you with fgets it is up to you

from question  

Reading large lists through stdin in C

We recommend using getline instead of fgets

from question  

Getline how to limit amount of input as you can with fgets

Edit as wolfer alluded to a null in your input will cause the string to be terminated prematurely when using fgets;getline is a better choice if available since it handles memory allocation and does not have issues with nul input

from question  

How to read the standard input into string variable until EOF in C?

Edit if you really want to use getline here s a version of your original program that ll work;but allocating your own memory and using fgets is better there s no call for using non-standard extensions when there s a perfectly good standard way of doing it even if you re using a third-party library like ncurses to begin with

from question  

Ncurses' printw() doesn't work

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0