0% found this document useful (0 votes)
26 views8 pages

Exp 12 PPS Lab

Uploaded by

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

Exp 12 PPS Lab

Uploaded by

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

EX.

NO :12 FILE OPERATIONS

1. Write a program in C to write multiple lines to a text file.

Aim

To write a C Program to demonstrate how to write multiple lines of text to a file.

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");

return 0; // Exit the program successfully


}

Output

This is the first line.


This is the second line.
This is the third line.
This is the fourth line.

Result

The program’s result is the creation of a text file that contains the lines of text as intended.

2. Write a program in C to find the number of lines in a text file.

Aim

Write a C Program to count the number of lines in a given 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 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");

// 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) {
// Increment line count when a newline character is encountered
if (ch == '\n') {
line_count++;
}
}
// If the file is not empty, increment line_count by 1 for the last line
if (ch != EOF) {
line_count++;
}
// Close the file
fclose(file);
// Print the total number of lines
printf("The number of lines in the file is: %d\n", line_count);
return 0; // Exit the program successfully
}

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.

The number of lines in the file is: 4

Result
The program successfully counts the number of lines in a text file.

3. Write a program in C to count the number of words and characters in a 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

Content of Input file


Hello world!
This is a C program.
It counts the number of words and characters.

Number of words: 10
Number of characters: 67

Result
The program prints the total number of words and characters found in the file.

4. Write a program in C to append multiple lines to the end of a text file.

Aim

To write a C program to append multiple lines to the end of a text file.

Algorithm

1. Open the file in append mode:


Open the target text file using the fopen() function with the mode "a" (append). This
ensures that data will be added to the end of the file without overwriting its current
content.
2. Check if the file was opened successfully:
If the file pointer is NULL, print an error message and exit the program.
3. Input multiple lines:
Continuously prompt the user for input, one line at a time, until the user decides to stop.
Use fgets() to read each line from the user.
4. Write the lines to the file:
For each line of input, use fputs() or fprintf() to write it to the file.
5. Close the file:
After writing all the lines, close the file using fclose().
6. End the program.

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

Enter multiple lines of text. Type 'exit' to stop.


Enter a line: Hello, this is the first line.
Enter a line: This is the second line.
Enter a line: exit
Lines appended successfully!

Hello, this is the first line.


This is the second line.

Result

Thus the C Program for appending multiple lines to the end of a text file completed
successfully.

5. Write a program in C to copy a file to another name.

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.

You might also like