| | |
remove newline character from a string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
hi all.
i have read a text line from a file using into a char array then i did
The output of is appearing on 2 lines instead on same line.
plz advise me wat i did wrong.
thanks.
i have read a text line from a file using
C Syntax (Toggle Plain Text)
fgets()
C Syntax (Toggle Plain Text)
char astr[30]
C Syntax (Toggle Plain Text)
strcat(astr,"secondstring")
The output of
C Syntax (Toggle Plain Text)
printf (astr)
plz advise me wat i did wrong.
thanks.
fgets places '\n' char at the end of string. Overwrite it by zero byte (end of C-string) - that's all:
c Syntax (Toggle Plain Text)
if (fgets(astr,sizeof astr,f)) { char* p = strchr(astr,'\n'); if (p) *p = '\0'; ... } else { /* eof or i/o error */ ... }
>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:
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:
c Syntax (Toggle Plain Text)
if ( fgets ( buf, sizeof buf, stdin ) != NULL ) { buf[strlen ( buf ) - 1] = '\0'; /* ... */ }
New members chased away this month: 4
ArkM's method is sufficient, but i prefer this way.
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
.
c Syntax (Toggle Plain Text)
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.
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.
Just food for thought.
New members chased away this month: 4
Narue,
Because
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? The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
Narue,
Becausefgets()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 ofstrlen()and a test?
c Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { char buffer[5]; // Arbitrary small size char *s = NULL; size_t n = 0; while ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) { size_t len = strlen ( buffer ); char *save = realloc ( s, n + len + 1 ); if ( save == NULL ) { perror ( "Insufficient memory" ); break; } strcpy ( save + n, buffer ); s = save; n += len; if ( buffer[len - 1] == '\n' ) { s[n - 1] = '\0'; break; } } printf ( ">%s<\n", s != NULL ? s : "(null)" ); free ( s ); return 0; }
c Syntax (Toggle Plain Text)
buffer[strlen ( buffer ) - 1] = '\0';
c Syntax (Toggle Plain Text)
size_t last = strlen ( buffer ) - 1; if ( buffer[last] == '\n' ) buffer[last] = '\0';
New members chased away this month: 4
>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.
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
![]() |
Similar Threads
- How to Remove Newline character from a VB String (Visual Basic 4 / 5 / 6)
- Problem in String Search:Please Help (C)
Other Threads in the C Forum
- Previous Thread: Download Turbo C
- Next Thread: Advice on client/server programs
Views: 2294 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






