How to add line break in c for files? The task is to get string And store it in file. While displaying it should print each string in new line. I have tried to add strcat (str,"\r\n") and also fprintf(fp,"%s\n",str) before writing each string to file.. But it displays empty.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct ch
{
char his[1000];
}s;
void main()
{
FILE *fp;
clrscr();
fp=fopen("str.txt","r+");
rewind(fp);
strcat(s.his,"text");
fwrite(&s,sizeof(s),1,fp);
fprintf(fp,"%s\n",s.his);
strcat(s.his,"new");
fwrite(&s,sizeof(s),1,fp);
rewind(fp);
while(1)
{
fread(&s,sizeof(s),1,fp);
if(feof(fp))
break;
printf("%s",s.his);
}
getch();
}
Expected Output:
text
new
Output i am getting just blank.