LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2005, 10:42 PM   #16
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282

Polishing naf's code
Code:
/* Get a line of text. */
fgets( buffer, 127, stdin );
You need to leave space for the string terminator.
 
Old 11-06-2005, 11:14 PM   #17
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Quote:
Originally posted by Wim Sturkenboom
Polishing naf's code
Code:
/* Get a line of text. */
fgets( buffer, 127, stdin );
You need to leave space for the string terminator.
Sorry Wim, you are incorrect here.

From the man page for fgets:
Code:
       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.
From MSDN .NET for fgets:
Code:
The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.
And, the most official source 'The C Programming Language', 2nd ed. by Brian W. Kernighan and Dennis M. Ritchie, pg 164:
Code:
The standard libarary provides an input routine fgets that is similiar to the getline function that we have used in earlier chapters:
char *fgets( char *line, int maxline, FILE *fp )
fgets reas the next input line (including the newline) from file fp into the character array line; at most maxline - 1 characters will be read. The resulting line is terminated with '\0'.  Normally fgets reutrns line; on end of file or error it returns NULL. (Our getline returns the line length, which is a more useful value; zero means end of file.)
This means if you try giving an input of 500 characters, only 127 will be stored and a '\0' (null character for C string termination) is append on the 128th spot (i.e., buffer[127] = '\0').

Side note, I highly recommend the book just mentioned.
 
Old 11-07-2005, 03:20 AM   #18
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
You're right. I overlooked it So used to using read where it needs to be done.

Last edited by Wim Sturkenboom; 11-07-2005 at 03:23 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
plz plz help me regarding route mapping nedian123 Programming 1 07-13-2004 08:17 AM
plz plz solve my route mapping problem nedian123 Linux - Networking 1 07-12-2004 09:41 PM
redhat problems plz plz help sap666 Linux - Newbie 5 07-30-2003 10:57 AM
I am THE beginner please help me :( statuszero Linux - General 10 05-26-2002 03:44 PM
beginner archangel Linux - Security 11 09-03-2001 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration