I am trying to use getc(character)
to take an element from a file and do stuff with it, but it appears that it must have a '\n'
before the end of a line is met.
How can I remove this so that when I copy the characters I don't have a new line character appearing anywhere - thus allowing me to deal with printing new lines when I choose?
.
.
.
#include <string.h>
.
. /* insert stuff here */
.
char* mystring = "THIS IS MY STRING\n"
char* deststring;
.
.
.
strncpy(deststring, mystring, strlen(mystring)-1);
.
.
.
(As an added note, I'm not a huge fan of dropping \0 characters in strings like that. It doesn't work well when you start doing i18n and the character width is not fixed. UTF-8, for example, can use anywhere from 1 to 4 bytes per "character".)
To replace all new line char with spaces, use:
char *pch = strstr(myStr, "\n");
while(pch != NULL)
{
strncpy(pch, " ", 1);
pch = strstr(myStr, "\n");
}
To remove first occurrence of new line char in string, use:
char *pch = strstr(myStr, "\n");
if(pch != NULL)
strncpy(pch, "\0", 1);
Hmm, wouldn't help to use getc to fill a buffer and remove newline and carriage return characters?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With