Exp 12 PPS Lab
Exp 12 PPS Lab
Aim
Algorithm
1. Start.
2. Open the file in write mode ("w"). If the file doesn't exist, it will be created.
3. Check if the file is opened successfully.
If the file is opened successfully, continue.
If not, print an error message and exit.
4. Write multiple lines to the file. Each line can be written using fprintf() or fputs() in C.
After writing all the lines, close the file.
5. End.
Program
#include <stdio.h>
int main() {
// Declare a file pointer
FILE *file;
// Open the file in write mode. If the file does not exist, it will be created.
file = fopen("output.txt", "w");
// Check if the file is opened successfully
if (file == NULL) {
// If the file couldn't be opened, print an error message and exit.
printf("Error opening the file.\n");
return 1; // Exit the program with an error code
}
// Write multiple lines to the file using fprintf or fputs
fprintf(file, "This is the first line.\n");
fprintf(file, "This is the second line.\n");
fprintf(file, "This is the third line.\n");
fprintf(file, "This is the fourth line.\n");
// Optionally, you can use fputs to write strings:
// fputs("This is another line.\n", file);
// Close the file after writing
fclose(file);
// Print a success message
printf("Data written to file successfully.\n");
Output
Result
The program’s result is the creation of a text file that contains the lines of text as intended.
Aim
Algorithm
1. Start.
2. Open the file in read mode ("r").
3. Check if the file is opened successfully.
If the file is not opened successfully, print an error message and exit.
4. Initialize a variable line_count to zero.
5. Use a loop to read the file line by line using fgets() or fgetc().
Each time a newline character \n is encountered, increment the line_count by 1.
6. After reaching the end of the file (EOF), close the file.
7. Print the value of line_count, which represents the total number of lines in the file.
8. End.
Program
#include <stdio.h>
int main() {
FILE *file;
char ch;
int line_count = 0;
// Open the file in read mode
file = fopen("input.txt", "r");
Output
Content of Input file
Hello, world!
This is a C program.
It counts the number of lines.
Each line is separated by a newline.
Result
The program successfully counts the number of lines in a text file.
Aim
Write a C Program to count the number of words and characters in a text file.
Algorithm
1. Start.
2. Open the file in read mode ("r").
3. Check if the file is opened successfully.
If the file is not opened successfully, print an error message and exit.
4. Initialize variables word_count and char_count to zero.
5. Use a loop to read the file character by character.
6. For each character:
a. Count characters: Increment char_count for each non-space, non-newline, and
non-tab character.
b. Count words: Increment word_count whenever a transition from a non-space
character to a space, tab, or newline occurs.
7. After reading all characters, close the file.
8. Print the total number of words and characters.
9. End.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file;
char ch;
int word_count = 0, char_count = 0;
int in_word = 0; // Flag to check if we are inside a word
// Open the file in read mode
file = fopen("input.txt", "r");
// Check if the file was opened successfully
if (file == NULL) {
// If file could not be opened, print an error message and exit
printf("Error opening the file.\n");
return 1; // Exit with an error code
}
// Read the file character by character
while ((ch = fgetc(file)) != EOF) {
// Count characters (including spaces, tabs, and newlines)
char_count++;
// Check for word boundaries (space, tab, or newline)
if (isspace(ch)) {
if (in_word) {
// If we encounter a space after a word, it marks the end of a word
word_count++;
in_word = 0; // Reset the in_word flag
}
} else {
// If the character is not a space, tab, or newline, we are inside a word
in_word = 1;
}
}
// If the file ends with a word, count the last word
if (in_word) {
word_count++;
}
// Close the file
fclose(file);
// Print the total number of words and characters
printf("Number of words: %d\n", word_count);
printf("Number of characters: %d\n", char_count);
return 0; // Exit the program successfully
}
Output
Number of words: 10
Number of characters: 67
Result
The program prints the total number of words and characters found in the file.
Aim
Algorithm
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char line[256];
char filename[] = "example.txt"; // Name of the file to append to
// Open the file in append mode
file = fopen(filename, "a");
// Check if the file was opened successfully
if (file == NULL) {
perror("Unable to open file");
return 1;
}
printf("Enter multiple lines of text. Type 'exit' to stop.\n");
// Continuously read input from the user until 'exit' is entered
while (1) {
printf("Enter a line: ");
fgets(line, sizeof(line), stdin); // Read a line of input
// Check if the user wants to exit
if (strncmp(line, "exit", 4) == 0) {
break;
}
// Write the line to the file
fputs(line, file);
}
// Close the file after appending
fclose(file);
printf("Lines appended successfully!\n");
return 0;
}
Ouput
Result
Thus the C Program for appending multiple lines to the end of a text file completed
successfully.
Aim
To write a C program to copy the contents of one file into another file.
Algorithm
1. Open the source file:
Use fopen() with the mode "r" to open the source file for reading.
2. Check if the source file was opened successfully:
If the source file pointer is NULL, print an error message and exit the program.
3. Open the destination file:
Use fopen() with the mode "w" to open the destination file for writing. If the file does
not exist, it will be created. If it exists, it will be overwritten.
4. Check if the destination file was opened successfully:
If the destination file pointer is NULL, print an error message and exit the program.
5. Read data from the source file:
Use a loop to read data from the source file, typically in chunks, and write it to the
destination file.
6. Write data to the destination file:
Use fputc() or fwrite() to copy the data from the source file to the destination file.
7. Close both files:
After the copy operation is complete, close both the source and destination files using
fclose().
8. End the program.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *sourceFile, *destFile;
char ch;
char sourceFileName[100], destFileName[100];
// Ask for the source file name and destination file name
printf("Enter the source file name: ");
scanf("%s", sourceFileName);
printf("Enter the destination file name: ");
scanf("%s", destFileName);
// Open the source file in read mode
sourceFile = fopen(sourceFileName, "r");
// Check if the source file was opened successfully
if (sourceFile == NULL) {
printf("Error: Could not open source file %s\n", sourceFileName);
return 1;
}
// Open the destination file in write mode
destFile = fopen(destFileName, "w");
// Check if the destination file was opened successfully
if (destFile == NULL) {
printf("Error: Could not open destination file %s\n", destFileName);
fclose(sourceFile);
return 1;
}
// Copy content from source file to destination file
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destFile);
}
// Close both files
fclose(sourceFile);
fclose(destFile);
printf("File copied successfully from %s to %s.\n", sourceFileName, destFileName);
return 0;
}
Output
Enter the source file name: source.txt
Enter the destination file name: destination.txt
File copied successfully from source.txt to destination.txt.
Result
Thus the program to copy the contents of one file into another file completed
successfully.