top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program to copy a file such that blank lines are not written to the new file.

+3 votes
647 views
Write a program to copy a file such that blank lines are not written to the new file.
posted Apr 30, 2016 by Ajay Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

–1 vote

Here is source code of the C Program to copy a file into another file. The C program is successfully compiled and run on a Linux system.
/*
* C Program to Copy a File into Another File
*/

include <stdio.h>

void main(int argc,char **argv)
{
FILE *fp1, *fp2;
char ch;
int pos;

if ((fp1 = fopen(argv[1],"r")) == NULL)    
{    
    printf("\nFile cannot be opened");
    return;
}
else     
{
    printf("\nFile opened for copy...\n ");    
}
fp2 = fopen(argv[2], "w");  
fseek(fp1, 0L, SEEK_END); // file pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // file pointer set at start
while (pos--)
{
    ch = fgetc(fp1);  // copying file character by character
    fputc(ch, fp2);
}    
fcloseall();    

}

answer Apr 30, 2016 by Devendra Bohre
This will copy whole file, What he asked is "copy a file such that blank lines are not written to the new file", Can you please share code?
Similar Questions
0 votes

I want to write a c program where I can count no of line, no of blank lines, no of commented lines and no of lines ending with semicolon, please help!!!

+3 votes

Is there a way to append a list of files as an executable to the file itself such that the file is still executable?

For example let's say I have 2 files file1 and file2. How might I append the the files in a way to the binary. Again this is a simple example, I would like to have a cross Operating System way to extract Unix and Windows functions. Cross operating system way to append zips (or tar.gz) to a file only to extract it later.

The following illustrates how the files might be extracted and or created.

create ./a.exe ./output.exe file1 file2

extract output.exe
...