Archived

This topic is now archived and is closed to further replies.

LiquidNRG

Carriage Returns In strcpy

Recommended Posts

LiquidNRG    122
Can anyone tell me if it is possible to copy data into a string and have carriage returns in it e.g. char str[128]; strcpy(str,"This is a long string which \n / I want to use carriage returns in \n / Can you help...?");

Share this post


Link to post
Share on other sites
dorix    484
quote:
Original post by YourOtherLeft
Can anyone tell me if it is possible to copy data into a string and have carriage returns in it e.g.

char str[128];
strcpy(str,"This is a long string which \n /
I want to use carriage returns in \n /
Can you help...?");



Close:

char str[128];
strcpy(str, "This is a long string which \n\
I want to use carriage returns in \n\
Can you help...?");

Note the backslashes at the ends of the lines, not forward slashes. Also note that there must NOT be any white spaces following those backslashes.

You could also do it the way Emptyhead suggested, but with the \n characters:

char str[128];
strcpy(str,"This is a long string which \n"
"I want to use carriage returns in \n"
"Can you help...?");


Either way works. Emptyhead is right, the compiler will concatenate the strings. You will have to insert the \n characters yourself, though. Emptyhead just misunderstood your question.

Finally, you don''t have to split the string across multiple lines at all:

char str[128];
strcpy(str,"This is a long string which \nI want to use carriage returns in \nCan you help...?");


Cheers, dorix

Share this post


Link to post
Share on other sites