0% found this document useful (0 votes)
48 views3 pages

Program 12

The document describes a C program to copy the contents of one text file to another. The program prompts the user to enter the names of the input and output files. It then opens the input file for reading, opens the output file for writing, reads each character from the input file and writes it to the output file, and closes both files once copying is complete. The program successfully copies the contents from one file to another as specified by the user.

Uploaded by

Shilpa Sannamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

Program 12

The document describes a C program to copy the contents of one text file to another. The program prompts the user to enter the names of the input and output files. It then opens the input file for reading, opens the output file for writing, reads each character from the input file and writes it to the output file, and closes both files once copying is complete. The program successfully copies the contents from one file to another as specified by the user.

Uploaded by

Shilpa Sannamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM 12

Write a C program to copy a text file to another, read both input file name and target
file name.

AIM:
To write a C program to copy a text file to another, read both input file name and
target file name.

ALGORITHM:

START

STEP 1:[ Input the file name to read ]


Read filename.
STEP 2: [Open file in read mode.]

fptr1=fopen(filename,”r”);
STEP 3: [Open file in write mode.]

fptr2=fopen(filename,”w”);

STEP 4: [Read character from source file and write to target file]

c = fgetc(fptr1);
fputc(c, fptr2);
STEP 5: Repeat STEP 4 until EOF.

STEP 6:[Close both files.]


fclose(fptr1);
fclose(fptr2);

STOP
FLOWCHART:

START

READ INPUT FILE


NAME

fptr1=fopen(filename,”r”)

True
fptr1=NULL? A

False

READ TARGET FILE


NAME

fptr2=fopen(filename,”w”)

True
fptr2=NULL? A

False

Read character from file1.

C=fgetc(fptr1;)

True
Is c=EOF? Close both files.

False A

Write character to file2.


STOP
fputc(fptr2,c)
PROGRAM:

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);

// Open one file for reading


fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);

// Open another file for writing


fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

// Read contents from file


c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

RESULT:
Thus the C program to copy a text file to another is executed successfully and output is
verified.

You might also like