user2884834
user2884834

Reputation: 129

Insert blank lines in output file C programming

I wrote a simple C program to copy the contact from input file to output one. It works fine. But now I need to insert blank lines between paragraphs in the new file and I can't get how to do this. Can anyone help me out?

Here is code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
   FILE *fp1,*fp2;
   char ch;


    fp1 =  fopen("input.txt","r");

    if(fp1==NULL)
    {
        printf("\nThe file was not found.");
        exit(1);
    }

    fp2 =  fopen("output.txt","w");

    if(fp2==NULL)
    {
        printf("\nThe file was not opened.");
        exit(1);
    }

    while(1)
    {
       ch = fgetc(fp1);

       if(ch==EOF)
          break;
       else
          putc(ch,fp2);
    }

    printf("File copied succesfully!");
    fclose(fp1);
    fclose(fp2);
}

Upvotes: 1

Views: 25603

Answers (3)

I found a solution to my problem (I simply wanted to have an empty line in the output between two lines.)

My solution is:

    `printf("\n");`

Upvotes: 1

Baldrick
Baldrick

Reputation: 11840

I suggest you try this:

while(1)
{
   ch = fgetc(fp1);

   if (ch == '\n')
      putc(ch,fp2);

   if(ch==EOF)
      break;
   else
      putc(ch,fp2);
}

Upvotes: 0

Simon
Simon

Reputation: 32893

Not sure if this is what you want but you can double \n characters like this:

if(ch==EOF)
    break;
else 
    putc(ch,fp2);

if(ch=='\n')
    putc(ch,fp2);

Upvotes: 0

Related Questions