0

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);
}
4

3 回答 3

1

我找到了解决问题的方法(我只是想在两行之间的输出中有一个空行。)

我的解决方案是:

    `printf("\n");`
于 2017-04-26T08:07:46.907 回答
0

我建议你试试这个:

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

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

   if(ch==EOF)
      break;
   else
      putc(ch,fp2);
}
于 2013-11-14T03:47:03.347 回答
0

不确定这是否是您想要的,但您可以\n像这样加倍字符:

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

if(ch=='\n')
    putc(ch,fp2);
于 2013-11-14T03:44:53.397 回答