Table of Contents
hide
Example : Write a Program(WAP) in C to create a New Blank File with a File Name and confirm its formation/creation.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp; // File Pointer declaration(fp) to store the address of first character of file contents
fp=fopen("D:\\student.txt","w");
if(fp==NULL)
{
printf("File Does Not Created");
exit(0);
}
printf("New Blank File Created Successfully.");
getch();
}
Output: New Blank File Created Successfully.
NB: New file is created with user given name 'student.txt' in the D drive.
Example : Write a Program(WAP) in C to create a New Blank File with a User-Defined File Name and confirm its formation/creation.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char filename[30];
printf("\nEnter the File Name You Want to Create : ");
scanf("%s",filename);
fp=fopen(filename,"w");
if(fp==NULL)
{
printf("File Does Not Created");
exit(0);
}
printf("New File created successfully.");
getch();
}
Output: New File created successfully.
NB: New file is created with user given name in the same directory/drive where the program file(say program1.c) is saved.
Example : Write a Program(WAP) in C to create a new blank file, and then write/store/append Static Data into it.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
fp = fopen("D:\\New folder\\abc.txt","w");
fprintf(fp, "Your Popular website is codershelpline for all computer students");
//fprintf(fp, "%s", "Your Popular website is codershelpline for computer students");
fclose(fp);
return 0;
}
NB : Here the static messages 'Your Popular website is codershelpline.com for all computer professionals' is stroed in the newly created "D:\\New folder\\abc.txt" file.
---------------- OR -----------------
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
char filename[30];
printf("\nEnter File Name You Want to Create : ");
scanf("%s",filename);
fp=fopen(filename,"w");
if(fp==NULL)
{
printf("File Does Not Created");
exit(0);
}
printf("New File created successfully.");
putc('C',fp); //writting single character into file
putc('H',fp);
putc('L',fp);
printf("\nData is stored/written in file successfully.\n");
fclose(fp);
fp=fopen(filename,"r");
printf("Contents of file is/are :\n");
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
getch();
}
Output :
Enter File Name You Want to Create : abc.txt
New File created successfully.
Data is stored/written in file successfully.
Contents of file is :
CHL
NB: 'Read mode' is used to display the contents of a file whereas 'Write mode' is for Store/Save/Add the contents in the file.
Example : WAP in C to store and display student records in a newly created file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char sname[80],ch;
int sage;
float fee;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter student name: \n");
scanf("%s", sname);
fprintf(fp, "student name = %s\n", sname);
printf("Enter student age: \n");
scanf("%d", &sage);
fprintf(fp, "student age is = %d\n", sage);
printf("Enter student course fee:\n");
scanf("%f", &fee);
fprintf(fp, "student fee is = %.2f\n", fee);
fclose(fp);
printf("\nThe output is : \n\n");
fp=fopen("student.txt","r");
while((ch=getc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp) ;
getch();
}
Output :
Enter student name:
Sohan
Enter student age:
35
Enter student course fee:
501.75
The output is :
student name = Sohan
student age is = 35
student fee is = 501.75
Example : A simple file handling program in C to display the output of already stored file contents.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp= fopen ("D:\\New folder\\abc.txt", "r");
if (fp==NULL)
{
puts("Unable to Open Existing File");
exit(0);
}
else
{
while(1)
{
ch = fgetc(fp); // To read/extract file contents character by character.
if(ch==EOF) // To check the contents of file till the end
break;
else
printf("%c", ch);
}
}
fclose(fp); // To close the file
getch();
}
/* ---------- OR ------------ */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp = fopen("F:\\New folder\\abc.txt", "r");
int ch = getc(fp);
while (ch != EOF)
{
putchar(ch);
ch = getc(fp);
}
if (feof(fp))
printf("\n Now the contents reached the end of file.");
else
printf("\n File contents traverse improperly");
fclose(fp);
getchar();
}
Output :
(NB : First of all 'abc.txt' file contents are displayed and then EOF message on execution the program.)
This is a codershelpline site for all
Now the contents reached the end of file.
Example : A file handling program in C to Count the Number of Characters in a file.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int count=0;
char ch;
fp = fopen("D:\\New folder\\abc.txt","w");
fprintf(fp, "%s", "Your Popular website is codershelpline.com for all computer professionals");
fclose(fp);
printf("\nThe Total Characters in the file is : \n\n");
fp=fopen("D:\\New folder\\abc.txt","r");
while((ch=getc(fp))!=EOF)
{
count++;
}
fclose(fp) ;
printf("%d",count);
return 0;
}
Output:
The Total Characters in the file is :
73
Example : A file handling program in C to Count the Number of Lines in a file.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int count=0;
char ch;
fp = fopen("D:\\New folder\\abc.txt","w");
fprintf(fp, "%s", "Your Popular \nwebsite is codershelpline.com\n for all computer professionals.");
fclose(fp);
fp=fopen("D:\\New folder\\abc.txt","r");
//Read character by character to check new line
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
count++;
}
fclose(fp);
printf("Total number of lines are : %d\n",count);
return 0;
}
Output:
Total number of lines are : 2
-------------- OR --------------
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int count=0;
char ch;
fp = fopen("D:\\New folder\\abc.txt","w");
fprintf(fp, "%s", "This is Your Popular website. It is codershelpline for all computer professionals.");
fclose(fp);
fp=fopen("D:\\New folder\\abc.txt","r");
//Read character by character to check new line
while((ch=fgetc(fp))!=EOF)
{
if(ch=='.')
count++;
}
fclose(fp);
printf("Total number of lines are : %d\n",count);
return 0;
}
Output:
Total number of lines are : 2
Example : A file handling program in C to Count the Number of Words in a file.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int count=0;
char ch;
fp = fopen("D:\\New folder\\abc.txt","w");
fprintf(fp, "%s", "This is Your Popular website.");
fclose(fp);
fp=fopen("D:\\New folder\\abc.txt","r");
//Read character by character to check new line
while((ch=fgetc(fp))!=EOF)
{
if(ch==' ')
count++;
}
fclose(fp);
printf("Total number of Words are : %d\n",count+1);
return 0;
}
Output:
Total number of Words are : 5
Example : WAP in C to create a new file, store data in it, and Rename the created file.
#include <stdio.h>
#include <conio.h>
void main( )
{
FILE *fp;
fp = fopen("prg1.c","w");
fprintf(fp, "%s", "Your website is codershelpline for all computer students");
fclose(fp);
rename("prg2.c","prg1.c");
printf("Created/Existing File is Renamed\n");
fp=fopen("prg1.c","r");
if(fp==NULL)
{
printf("File is not Present\n");
}
else
{
printf("File is Present\n");
}
fclose(fp);
getch();
}
Output:
Created/Existing File is Renamed
File is Present
Example : WAP in C to Remove/Delete a created/existing file.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include<ctype.h>
void main( )
{
FILE *fp;
fp = fopen("prg1.c","w");
fprintf(fp, "%s", "Your website is codershelpline for all computer students");
fclose(fp);
remove("prg1.c");
printf("Created/Existing File is Deleted\n");
fp=fopen("prg1.c","r");
if(fp==NULL)
{
printf("File is not Present\n");
}
getch();
}
Example : How to Copy the contents of one file into another file and display them?
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main( )
{
FILE *fp,*fp1 ;
char str[500],ch;
fp = fopen("abc.c", "w") ; //Create a new file & open in write mode.
printf("Enter the characters for new file below 500\n");
while (strlen(gets(str))>0)
{
fputs(str, fp) ;
fputs("\n", fp) ;
}
fclose(fp) ;
fp1=fopen("simple.c","w");
fp = fopen("abc.c", "r") ;
while((ch=fgetc(fp))!=EOF)
{
putc(ch,fp1);
//fputc(ch,fp1);
}
fclose(fp) ;
fclose(fp1) ;
fp1=fopen("simple.c","r");
printf( "The copied values are :\n" ) ;
while((ch=getc(fp1))!=EOF)
{
printf("%c",ch);
}
fclose(fp1) ;
getch();
}
NB: Press two time Enter button to stop to take input below 150 characters.
Example : How do you convert lowercase letters in one file into uppercase letters in another and display them?
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main( )
{
FILE *fp,*fp1 ;
char str[150],ch;
fp = fopen("abc.c", "w") ; //Create a new file & open in write mode.
printf("Enter the characters for new file below 150\n");
while (strlen(gets(str))>0)
{
fputs(str, fp) ;
fputs("\n", fp) ;
}
fclose(fp) ;
fp1=fopen("simple.c","w");
fp = fopen("abc.c", "r") ;
while((ch=fgetc(fp))!=EOF)
{
if(islower(ch))
{
ch=ch-32;
}
putc(ch,fp1);
}
fclose(fp) ;
fclose(fp1) ;
fp1=fopen("simple.c","r");
printf( "The outputs are \n" ) ;
while((ch=getc(fp1))!=EOF)
{
printf("%c",ch);
}
fclose(fp1) ;
getch();
}
Example : How do you Merge two files’ contents into another file and display them?
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main( )
{
FILE *fp1,*fp2,*fp3 ;
char str1[150],str2[150],str3[300],ch;
fp1 = fopen("abc.c", "w") ; //Create a new file & open in write mode.
printf("Enter the characters in first file below 150 size\n");
while (strlen(gets(str1))>0)
{
fputs(str1, fp1) ;
fputs("\n", fp1) ; //sends cursor in new line after insertion of newline characters.
}
fclose(fp1) ;
fp2 = fopen("simple.c", "w") ; //Create a new file & open in write mode.
printf("Enter the characters in second file below 150 size\n");
while (strlen(gets(str2))>0)
{
fputs(str2, fp2) ;
fputs("\n", fp2) ;
}
fclose(fp2) ;
fp3=fopen("xyz.c","w");
fp1=fopen("abc.c","r");
while((ch=fgetc(fp1))!=EOF)
{
putc(ch,fp3);
}
fclose(fp1) ;
fp2=fopen("simple.c","r");
while((ch=fgetc(fp2))!=EOF)
{
putc(ch,fp3);
}
fclose(fp2) ;
fclose(fp3) ;
fp3=fopen("xyz.c","r");
printf( "The merged values of two files are :\n") ;
while((ch=getc(fp3))!=EOF)
{
printf("%c",ch);
}
fclose(fp3) ;
getch();
}
Output :
Enter the characters in first file below 150 size
www.codershelpline.com
Enter the characters in second file below 150 size
It is a very good site for computer knowledge.
The merged values of two files are :
www.codershelpline.com
It is a very good site for computer knowledge.
Example : How do you display the Reverse of file contents?
#include <stdio.h>
int main()
{
FILE *fp;
long file_size;
char ch;
long i;
fp = fopen("example.txt","w");
fprintf(fp, "Your Popular website is codershelpline for all computer students");
fclose(fp);
fp = fopen("example.txt", "r"); // Open file in read mode
if (fp == NULL)
{
printf("Error in opening file.\n");
}
else
{
fseek(fp, 0, SEEK_END); // Move to end of file
file_size = ftell(fp); // Get the size of the file stored on the disk
for (i = file_size - 1; i >= 0; i--)
{
fseek(fp, i, SEEK_SET); // Move pointer to specific position
ch = fgetc(fp); // Read character
printf("%c", ch); // Print in reverse order
}
fclose(fp); // Close the file
}
return 0;
}
Output: stneduts retupmoc lla rof enilplehsredoc si etisbew ralupoP ruoY
Example : How do you get the Size of the File stored on the disk?
#include <stdio.h>
int main()
{
FILE *fp;
long file_size;
fp = fopen("example.txt","w");
fprintf(fp, "Your Popular website is codershelpline for all computer students");
fclose(fp);
fp = fopen("example.txt", "r"); // Open fp in read mode
if (fp == NULL)
{
printf("Error in opening file.\n");
}
else
{
// Move the file pointer to the end of the file
fseek(fp, 0, SEEK_END);
file_size = ftell(fp); // Get the size of the file stored on the disk
printf("The size of file on the disk is %ld bytes",file_size);
fclose(fp); // Close the file
}
return 0;
}
Output: The size of file on the disk is 64 bytes.
NB: The program uses 'fseek' to jump to the end of the file and 'ftell' to get the position of the file pointer, which indicates the size of the file.
Example : A file handling program in C to display its own written source code on the screen as output.
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen(__FILE__,"r");
do
{
ch = getc(fp);
putchar(ch);
}
while(ch != EOF);
fclose(fp);
return 0;
}
NB :This program displays the content of file source code due to use of __FILE__ that contains the location of this C programming file in a string.
0 Comments