Write a program to concatenate the contents of two files into a third file.

#include <stdio.h>

int main(int argc , char *argv[])
{
FILE *src1, *src2, *target;
char ch;

if(argc != 4)
{
printf("\nToo few arguments. You must specify the filenames.");
return 1;
}

src1 = fopen(argv[1], "rb");
src2 = fopen(argv[2], "rb");
target = fopen(argv[3], "wb");
if(src1 == NULL || src2 == NULL || target == NULL)
{
printf("\nUnable to open \'%s\'",
src1 ? src2 ? argv[3] : argv[2] : argv[1]);
if(src1) fclose(src1);
if(src2) fclose(src2);
if(target) fclose(target);
return 1;
}

while(!feof(src1))
{
ch = fgetc(src1);
fputc(ch, target);
}

while(!feof(src2))
{
ch = fgetc(src2);
fputc(ch, target);
}


printf("\n\'%s\' and \'%s\' concatenated to \'%s\' successfully", argv[1], argv[2], argv[3]);

fclose(src1);
fclose(src2);
fclose(target);

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin