Chapter:
1. Write C program to copy contents of one file to anor file.
include
#include
// For exit() void 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);
}
2. Write a C Program to merge contents of two files into a third file.
#include
#include
void main()
{
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
// Copy contents of first file to file3.txt
while ((c = fgetc(fp1)) != EOF)
{
fputc(c, fp3);
}
// Copy contents of second file to file3.txt
while ((c = fgetc(fp2)) != EOF)
{
fputc(c, fp3);
}
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
3. Write a C Program to print contents of file.
#include
#include
// For exit() void main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
}
4. Write a C Program to count Number of Characters in a File
#include
#include
// For exit() #define MAX_FILE_NAME 100
void main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
printf("Enter file name: ");
scanf("%s", filename);
// Open the file
fp = fopen(filename, "r");
// Check if file exists
if (fp == NULL) {
printf("Could not open file %s",
filename);
exit(0);
}
// Extract characters from file
// and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
{
// Increment count for this character
count = count + 1;
}
// Close the file
fclose(fp);
// Print the count of characters
printf("The file %s has %d characters\n ",
filename, count);
}
All Chapters
C Programs on integers
C programs on various integers
Recursive program in c
Arrays
- c programms on arrays
- complete IOE solution on arrays
- Detailed description
- And much more
Strings in C programming
- c programms on Strings
- complete IOE solution on Strings
- Detailed description
- Each one using pointer Concepts
- And much more
Data Files
- C programming concepts of data files and data structures
- IOE C Programming Solutions
- Important Numerous C Program
Guest