The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
removing trailing newline in C
Discuss removing trailing newline in C in the C Programming forum on Dev Shed. removing trailing newline in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 12th, 2002, 09:48 AM
|
 |
Contributing User
|
|
Join Date: Jan 2002
Posts: 32
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
removing trailing newline in C
currently i'm using fgets() to get a line from the standard input stream (stdin)... but i do not want the trailing newline that fgets() returns to me together with the line... how do i go about removing that trailing newline?
what i'm currently doing is iterating over every character in the character array and, if it's a '\n' or '\r' character, i replace it with the terminating null character '\0'
Code:
/* strip trailing newline */
for (i = 0; i < strlen(inputline); i++)
{
if ( inputline[i] == '\n' || inputline[i] == '\r' )
inputline[i] = '\0';
}
will this cause any problems? and is there a better way to do this?
thanks
|

October 12th, 2002, 10:16 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Well, since fgets() reads until it either encounters a newline or until the buffer is full, there'll never be a newline hidden somewhere in the string, but only at the end:
Code:
fgets(buf, sizeof(buf), stream);
if (stream[strlen(stream) - 1] == '\n') {
stream[strlen(stream) - 1] == '\0';
}
If you also want to get rid of carriage returns, then you'd have a reason to scan the entire string.
However, simply replacing every occurrence with a NUL-byte will get confusing. For instance, assume the buffer is 20 bytes large, and a 12-byte string is read, with a carriage return ('\r') byte at index 5 (the 6th character). By replacing it with '\0', you pretty much throw away the rest of the string, since there's no way to differentiate between the NUL byte inserted by fgets() and the NUL byte you replaced the '\r' with.
So you should probably replace it with another character (a space, perhaps), or delete it by shifting all the following characters one place to the left.
Just my $0.02 
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

October 12th, 2002, 11:36 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|

October 13th, 2002, 12:27 AM
|
 |
Contributing User
|
|
Join Date: Jan 2002
Posts: 32
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
thanks guys
i'd seen the fgetln() function while looking thru the man pages before but it seemed to cause roughly the same problems since now i'd have to still remove the newline character (every input line comes with a terminating newline) and add the terminating NUL char
i think i'll use Analyser's way
|

October 30th, 2002, 08:22 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
Another solution:
char* nl;
char mydata[80];
fgets(mydata, 80, stdin)
nl = strrchr(mydata, '\r');
if (nl) *nl = '\0';
nl = strrchr(mydata, '\n');
if (nl) *nl = '\0';
That will cream the trailing \r, and if there is a spurious \n before that, it'lll get creamed to. The other proposed solution looks faster.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|