C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 12th, 2002, 09:48 AM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old October 12th, 2002, 10:16 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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/

Reply With Quote
  #3  
Old October 12th, 2002, 11:36 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 20 m 32 sec
Reputation Power: 4080
You can also try out the fgetln() function (if it exists for your compiler), which will save you a call to strlen().
http://www.openbsd.org/cgi-bin/man....OpenBSD+Current has more documentation

Reply With Quote
  #4  
Old October 13th, 2002, 12:27 AM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old October 30th, 2002, 08:22 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > removing trailing newline in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap