0

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.

Prethiv
  • 27
  • 5
  • Have you heard about indenting code to make it readable? – Ed Heal Oct 06 '16 at 13:01
  • its alrdy indented. – Prethiv Oct 06 '16 at 13:03
  • 3
    strcat(s.his,"text"); s.his has not been initialized. This invokes undefined behaviour, please initialize it before working with it. – robin.koch Oct 06 '16 at 13:03
  • what is type of `his[1000]` ?????? – Manthan Tilva Oct 06 '16 at 13:04
  • It does not appear to be so - perhaps change tabs to spaces? – Ed Heal Oct 06 '16 at 13:04
  • the data type for his is char. – Prethiv Oct 06 '16 at 13:07
  • 3
    @robin.koch `s` is initialized. Because `s` is global variable. – BLUEPIXY Oct 06 '16 at 13:07
  • 1
    Does `str.txt` exist, or is the `fopen` failing? Omitting error checking for the purpose of simplifying an example is sometime laudable, other times it is the root of all evil. – William Pursell Oct 06 '16 at 13:17
  • You write "text" followed by 996 zeros (perhaps more, if there is any padding), then you write "text" followed by a newline then "textnew" followed by 993 zeros. Stop writing all the zeros. And don't use `feof` like this. It is incorrect, and your code will enter an infinite loop on a read error. http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Oct 06 '16 at 13:22
  • Best to only use `fread()/fwrite()` when opening files in binary mode. `"rb+"`. For this task use text more and `fprintf()`/ `fgets()` to write /read the text file. UV for a better than usual first time presentation of a problem. – chux - Reinstate Monica Oct 06 '16 at 13:33
  • After opening the file, test if `fp == NULL`. `"r+"` expects the file to exist. Is that the problem? Try using `"wb+"` to create a new file. – chux - Reinstate Monica Oct 06 '16 at 13:37
  • Your first `fwrite()` writes 1000 bytes, of which 4 are letters and 996 are null bytes. Your first `fprintf()` then adds 5 more bytes, 4 letters and newline (or probably 6 bytes since you're probably on Windows, as it converts the newline in the string to CRLF). Your second `fwrite` writes another 1000 bytes, 7 letters and 993 null bytes. Your file contains `"text"`, nulls, `"text`", newline, `"textnew"`, nulls. You don't force out a newline at the end of the reading cycle (but should). Remember, `fwrite()` and `fread()` are for writing and reading binary data and care nothing about lines. – Jonathan Leffler Oct 06 '16 at 14:17

0 Answers0