0% found this document useful (0 votes)
36 views5 pages

File Copy and Count Programs

C programming

Uploaded by

sukhmanpyaarii
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)
36 views5 pages

File Copy and Count Programs

C programming

Uploaded by

sukhmanpyaarii
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/ 5

Name: Sukhmanpreet kaur ROLL.

NO: 2324807

PRACTICAL NO.-29
Aim: Write a program to read the contents of a file and copy these contents to another file.
Software Used: CODE Blocks
Procedure:
1: The program declares two file pointers, source_file and destination_file.
2: The program opens the source file in read mode ("r").
3: The program checks if the source file was opened successfully. If not, it prints an error
message and exits.
4: The program opens the destination file in write mode ("w").
5: The program checks if the destination file was opened successfully. If not, it prints an error
message, closes the source file, and exits.
6: The program reads the contents of the source file character by character using fgetc().
7: The program writes each character to the destination file using fputc().
8: The program repeats steps 6-7 until the end of the source file is reached.
9: The program closes both the source and destination files using fclose().
10: The program prints a success message to the console.

Source code:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Declare file pointers
FILE *source_file, *destination_file;
// Open the source file in read mode
source_file = fopen("source.txt", "r");
if (source_file == NULL) {
printf("Error opening source file\n");
return 1; }
Name: Sukhmanpreet kaur ROLL.NO: 2324807

// Open the destination file in write mode


destination_file = fopen("destination.txt", "w");
if (destination_file == NULL) {
printf("Error opening destination file\n");
fclose(source_file);
return 1;
}
// Read and copy contents from source file to destination file
char c;
while ((c = fgetc(source_file)) != EOF) {
fputc(c, destination_file);
}
// Close the files
fclose(source_file);
fclose(destination_file);
printf("File open successfully and copied successfully\n");
return 0;
}

Output:
Name: Sukhmanpreet kaur ROLL.NO: 2324807

PRACTICAL NO.-30
Aim: Write a program to count characters, words and lines in a text file
Software Used: CODE Blocks
Procedure:
1: The program declares a file pointer file and opens the file example.txt in read mode ("r").
2: The program initializes three counters: char_count, word_count, and line_count, to keep track
of the number of characters, words, and lines in the file.
3: The program reads the file character by character using fgetc().
4: For each character read, the program increments the char_count.
5: If the character read is a newline character ('\n'), the program increments the line_count.
6: If the character read is a space or newline character, the program increments the word_count.
7: The program repeats steps 3-6 until the end of the file is reached.
8: After reading the entire file, the program increments the word_count one more time to account
for the last word.
9: The program prints the counts to the console, including the number of characters, words, and
lines.
10: Finally, the program closes the file using fclose().

Source code:
#include <stdio.h>
#include <stdlib.h>

int main() {
// Declare file pointer
FILE *file;
// Open the file in read mode
file = fopen("example.txt", "r");
Name: Sukhmanpreet kaur ROLL.NO: 2324807
if (file == NULL) {

printf("Error opening file\n");


return 1;
}

// Initialize counters
int char_count = 0;
int word_count = 0;
int line_count = 0;

// Read the file character by character


char c;
while ((c = fgetc(file)) != EOF) {
// Increment character count
char_count++;

// Check for newline character to increment line count


if (c == '\n') {
line_count++;
}

// Check for space or newline character to increment word count


if (c == ' ' || c == '\n') {
word_count++;
}
}
// Increment word count for the last word
Name: Sukhmanpreet kaur ROLL.NO: 2324807
word_count++;
// Print the counts

printf("Characters: %d\n", char_count);


printf("Words: %d\n", word_count);
printf("Lines: %d\n", line_count);

// Close the file


fclose(file);

return 0;
}

Output:

You might also like