Reputation: 293
I have a few lines of code:
strcat(myTxt,"data");
strcat(myTxt,"\n");
strcat(myTxt,"data1");
In between the lines I've done strcat
of "\n"
; however, when I do a write to a text file the "\n"
is ignored and all the strings are concatenated as datadata1
. How can I work around this issue?
Upvotes: 4
Views: 45244
Reputation: 14078
This code works for me:
#include <string.h>
#include <stdio.h>
int main ()
{
char myTxt[100];
myTxt[0] = 0;
strcat(myTxt, "data");
strcat(myTxt, "\n");
strcat(myTxt, "data1");
printf("%s\n", myTxt);
return 0;
}
Did you initialize the buffer's first byte? Edit: works also with a file as output:
#include <string.h>
#include <stdio.h>
int main ()
{
char myTxt[100];
FILE *out = fopen("out.txt", "wt");
myTxt[0] = 0;
strcat(myTxt, "data");
strcat(myTxt, "\n");
strcat(myTxt, "data1");
fprintf(out, "%s\n", myTxt);
fclose(out);
return 0;
}
Upvotes: 6