You are not logged in.

#1 2009-01-11 02:38:13

dsr
Member
Registered: 2008-05-31
Posts: 187

fgets vs. getline in C

For reading a line from stdin, do most people use the unsafe but standard fgets or the safe but gcc-specific (or at least non-standard) getline?

Offline

#2 2009-01-11 04:50:03

isak
Member
Registered: 2008-02-05
Posts: 18

Re: fgets vs. getline in C

how exactly would fgets be unsafe ? the second argument to fgets is the buffer size, gets on the other hand can give you troubles.

Offline

#3 2009-01-11 05:21:33

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: fgets vs. getline in C

Oh, a bunch of sites I was browsing said you can only use fgets when you've checked to make sure there's no null character in the input.

Offline

#4 2009-01-11 15:20:14

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: fgets vs. getline in C

fgets() shouldn't care about  '\0's.

man fgets wrote:

fgets() reads in at most one less than size characters from stream and stores them  into  the  buffer
pointed  to  by s.  Reading stops after an EOF or a newline.  If a newline is read, it is stored into
the buffer.  A '\0' is stored after the last character in the buffer.

But of course -- this means that the input stored in your buffer might contain '\0'-s. So perhaps that's what was meant on your nameless forums (:

Offline

#5 2009-01-11 17:29:47

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: fgets vs. getline in C

All right, so C programmers generally use fgets? gnud, I never said anything about forums. One such site was <http://www.gnu.org/software/libtool/man … Input.html> wink

Offline

#6 2009-01-14 00:10:50

e_tank
Member
Registered: 2006-12-21
Posts: 80

Re: fgets vs. getline in C

fgets is safe and can handle '\0's just fine, the reason for the warning is due to the fact that fgets places a '\0' in the buffer to indicate the last character it read, rather than returning the number of characters read.  so unless you know how much data to expect beforehand or you initialize your buffer with some other character that you're sure is not in the file, then if your file contains a '\0' you will not be able to tell what was the last character read in by fgets.

for example, say you have a buffer of 6 chars all set to '*'
you also have a file with the following characters
'a' 'b' '\0' 'd' 'e'
calling fgets(buf,5,file) will leave the following data in your buffer
'a' 'b' '\0' 'd' '\0' '*'
since the buffer was initialized with '*'s we know the second '\0' is the one fgets left to mark the last character read in.  if it was initialized with '\0's or random data and you didn't know how much data to expect, then you wouldn't be able to know this for sure

edit: whoops, wouldn't have posted this if i had noticed gnud had already given an explanation for this

Last edited by e_tank (2009-01-14 00:19:34)

Offline

#7 2009-01-14 00:41:09

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: fgets vs. getline in C

Ah, that makes sense. Thanks for the explanation.

Offline

Board footer

Powered by FluxBB