943,677 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8558
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 5th, 2009
0

remove newline character from a string

Expand Post »
hi all.

i have read a text line from a file using
  1. fgets()
into a char array
  1. char astr[30]
then i did
  1. strcat(astr,"secondstring")

The output of
  1. printf (astr)
is appearing on 2 lines instead on same line.

plz advise me wat i did wrong.
thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
May 5th, 2009
0

Re: remove newline character from a string

fgets places '\n' char at the end of string. Overwrite it by zero byte (end of C-string) - that's all:
  1. if (fgets(astr,sizeof astr,f)) {
  2. char* p = strchr(astr,'\n');
  3. if (p)
  4. *p = '\0';
  5. ...
  6. } else { /* eof or i/o error */
  7. ...
  8. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 5th, 2009
2

Re: remove newline character from a string

>fgets places '\n' char at the end of string.
To the OP: ArkM's code does the right thing, but the quoted statement is incomplete. fgets preserves the newline, but not if the buffer is filled. So solutions like the following distressingly common one are incorrect:
  1. if ( fgets ( buf, sizeof buf, stdin ) != NULL ) {
  2. buf[strlen ( buf ) - 1] = '\0';
  3.  
  4. /* ... */
  5. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 5th, 2009
0

Re: remove newline character from a string

ArkM's method is sufficient, but i prefer this way.

  1. buf[strcspn(buf,'\n')] = '\0';

it's just a single line of code that will replace the newline with a NULL, if a newline is found, or will do nothing if one is not there

.
Last edited by jephthah; May 5th, 2009 at 11:29 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 5th, 2009
2

Re: remove newline character from a string

>ArkM's method is sufficient, but i prefer this way.
Keep in mind that of the potential ways to do this, strcspn is among the least efficient while strchr is among the most efficient.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 5th, 2009
0

Re: remove newline character from a string

oh, i wasnt aware of that.

i guess the only time i ever concern myself with reducing cycle times is in embedded applications, and there i rarely deal with strings and newlines.

but thats useful to know. thanks.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 5th, 2009
2

Re: remove newline character from a string

I prefer using strcspn myself, but knowing the performance characteristics is important. However, none of the methods for stripping a newline will change the fact that the whole operation is I/O bound because of fgets. You can save cycles when stripping the newline, but it isn't likely to have a noticeable effect on performance as a whole.

Just food for thought.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 6th, 2009
0

Re: remove newline character from a string

Narue,
Because fgets() reads a line up to the \n and loads everything up to the RETURN, isn't it simply a matter of testing the last character in the buffer with \n and if it isn't, they entered more than the requested data? Simple use of strlen() and a test?
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
May 6th, 2009
3

Re: remove newline character from a string

Click to Expand / Collapse  Quote originally posted by WaltP ...
Narue,
Because fgets() reads a line up to the \n and loads everything up to the RETURN, isn't it simply a matter of testing the last character in the buffer with \n and if it isn't, they entered more than the requested data? Simple use of strlen() and a test?
Yes, what's your point? If all you want to do is determine if a partial line was read, then looking for a newline is trivial. Ideally one would write code that can handle both cases where fgets reads a newline and where it doesn't. For example:
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. char buffer[5]; // Arbitrary small size
  6. char *s = NULL;
  7. size_t n = 0;
  8.  
  9. while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
  10. size_t len = strlen ( buffer );
  11. char *save = realloc ( s, n + len + 1 );
  12.  
  13. if ( save == NULL ) {
  14. perror ( "Insufficient memory" );
  15. break;
  16. }
  17.  
  18. strcpy ( save + n, buffer );
  19. s = save;
  20. n += len;
  21.  
  22. if ( buffer[len - 1] == '\n' ) {
  23. s[n - 1] = '\0';
  24. break;
  25. }
  26. }
  27.  
  28. printf ( ">%s<\n", s != NULL ? s : "(null)" );
  29. free ( s );
  30.  
  31. return 0;
  32. }
But if you're not worrying about long lines, you still need to consider the case where a newline may not be present, which is what I was talking about with the usual naive strlen solution:
  1. buffer[strlen ( buffer ) - 1] = '\0';
Unless this is qualified with a test to verify that a newline is present at that index, you would be removing valid data. An extra test is needed to make it work for long lines:
  1. size_t last = strlen ( buffer ) - 1;
  2.  
  3. if ( buffer[last] == '\n' )
  4. buffer[last] = '\0';
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 6th, 2009
0

Re: remove newline character from a string

>Simple use of strlen() and a test?
strlen() has to itinerate through the string to give the length, to that
the check has to be performed to find that '\n'.

strchr() has to itinerate through the string looking for the given char.
Hard for me to see which is faster.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Download Turbo C
Next Thread in C Forum Timeline: Advice on client/server programs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC