1

This is my first C class. I need to write a program that copies one file content into another and add an empty line after line 7 in output file. I wrote a code that add empty line after each line, but I need only one line. Please, can anyone help. Here is what I got so far:

#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("outfile.txt","w");

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

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

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

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

    printf("File copied succesfully!");
    fclose(fp1);
    fclose(fp2);
}
user2884834
  • 129
  • 4
  • 9
  • 14
  • If you cannot open the input file, it is not necessarily because the "file was not found". It is better to let the system tell you the reason for the error than to assume a reason. Also, error messages should be written to stderr rather than stdout. It is easy to accomplish both: `char *path="input.txt"; f=fopen( path, "r" ); if( f == NULL ) {perror( path ); exit(EXIT_FAILURE);}` – William Pursell Nov 17 '13 at 03:06

1 Answers1

1

You're correct that you are currently adding an extra newline character every time you find one. Instead, you need a way to identify only the 7th line. It sounds to me like you can count the newlines, then only do the extra newline placement after the seventh one. Maybe add another integer to the top of your main function:

int newline_count = 0;

Increment it in your loop, and only add the newline character when you need it:

if (ch == '\n'){

    ++newline_count; /* increments newline_count by 1 */

    if (newline_count == 7){   

        putc(ch, fp2);

    }
}

A couple of notes:

1) You'll notice that I have brackets on my second IF statement that are technically unnecessary. It's purely a style choice on my part, and you can leave them off if you want, but if you should still indent the statement that the IF statement controls, like this:

    if (newline_count == 7)
        putc(ch, fp2);

In your code, your third IF statement has the same indentation as the statement it controls. It will be much more readable if you indent the second statement.

2) Your main function should never be void. You should at least have this:

int main(){

    /* ... stuff ... */

    return (0);
}

See the first answer to this question for more information about how main() should look and more importantly, why.

3) I think your whole loop could look a little cleaner if you reordered your if statements:

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

   if (ch == EOF)
      break;

   if (ch == '\n'){
        ++newline_count; /* incremement count of newlines by 1 */

        if (newline_count == 7){   
            putc('\n', fp2); /* insert a newline character */
        }
    }

     putc(ch, fp2);
}

By reordering things, you've gotten rid of an extra "else" statement. You didn't really need it in the first place since you would break out of the loop once EOF was reached. Also, by changing the putc(ch, fp2) to putc('\n', fp2) you've more clearly identified your intention to someone reading your code. Your goal isn't just to copy the same character again, it's to put a new line in there.

Community
  • 1
  • 1
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84