Example : Write a program (WAP) in C to create new blank file with a name in C.
#include<stdio.h>
#include<conio.h>
void main()
{ 
    FILE *fp;  
    char Str[30];
 
    printf("Enter the file name we want to create : ");
    scanf("%s",Str); 
    
    fp=fopen(Str,"w");
   
    if(fp==NULL)
    {
        printf("File Not Created");
        exit(0); 
    } 
    printf("File is created successfully.");
    getch();
}

Output: 
Enter the file name we want to create : abc
File created successfully.
(NB : New file 'abc' of unknown type is created in the same directory where program is saved.)

Enter the file name we want to create : abc.txt
File created successfully.
(NB : New Notepad file 'abc.txt' is created in the same directory where program is saved.)

Enter the file name we want to create : abc.docx
File created successfully.
(NB : New Word file 'abc.docx' is created in the same directory where program is saved.)

Enter the file name you want to create : F:\abc.txt
File created successfully.
(NB : New file 'abc.txt' is created in the F drive of the system.)
Example : WAP in C to check whether a file is present/created or not.
#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp;    /* File Pointer declaration(fp) to store the address of first character of file 
                     contents */    
    char c;     
    fp= fopen ("F:\\New folder\\abc.txt", "r");    // To Open an existing file in Read mode(r)
    fp= fopen ("F:\\New folder\\abc.txt", "w");    // To Open an existing file in Write mode(w) 
   
    if (fp==NULL)
      {
    	puts("File does not exists");
    	exit(0);
      } 
    else 
      {	      
        puts("File Exists");
      }
    fclose(fp);  // To close the file
    getch();
}

NB : 'Read mode' is used to display the contents whereas 'Write mode' is for Store/save the contents in the file.
Example : WAP in C to store certain string messages into a file.
#include<stdio.h>
#incluce<conio.h>
int main()
{
    FILE *fp;
    fp = fopen("F:\\New folder\\abc.txt","w");
    fprintf(fp, "%s", "This is a codershelpline site for all");
    fclose(fp);
    return 0; 
}

NB : Here the string messages 'This is a codershelpline site for all' is stroed in "F:\\New 
     folder\\abc.txt" the file.
Example : WAP in C to create new file, write/store data into it and display them.
#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

/*  --------------  OR  -----------------*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
 
void main( )
{
    FILE *fp ;
    char str[150];   
    
    fp = fopen("abc.c", "w") ;    //Creates a new file & opens in write mode.

    printf("Enter the characters for new file below  150 size\n");
    while (strlen(gets(str))>0)
    {
        fputs(str, fp) ;   
        fputs("\n", fp) ;
    }    
    fclose(fp) ;
    
    fp = fopen("abc.c", "r") ;    
    printf( "The outputs are" ) ;
    while( fgets ( str, 150, fp ) != NULL )
     printf( "%s" , str ) ;
	      
    fclose(fp) ;    
   getch();       
}

NB : Enter data line by line by pressing enter key but to come out from it, wait few seconds and then    
     press enter button.
Example : WAP in C to store student records in a file and display them.
#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 file contents.
#include <stdio.h>
#include<conio.h>
void main()
{
    FILE *fp;    /* File Pointer declaration(fp) to store the address of first character of file 
                     contents */    
    char ch;     
    fp= fopen ("G:\\New folder\\abc.txt", "r");    // To Open an existing file in Read mode(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 message is displayed and then EOF message.) 
 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 lines present in a file.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include<stdlib.h>
 
void main( )
{
    FILE *fp ;
    char str[150],ch;
    int count=0;   
    
    fp = fopen("abc.c", "w") ;    //Create a new file & open in write mode.
    printf("Enter the characters for new file below size 150\n");
    while (strlen(gets(str))>0)
    {
        fputs(str, fp) ;   
        fputs("\n", fp) ;  // insert new line after pressing enter key during input and pressing twice enter key without typing anything to come out from input.
    }    
    fclose(fp) ;
    
    fp = fopen("abc.c", "r") ;	
	if(fp==NULL)
	{
		printf("File does not exist!!!\n");
		exit(0);
	}

	//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);
	   
   getch();       
}
Example : WAP in C to create new file, store data in it and rename the created file.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include<ctype.h>
 
void main( )
{
    FILE *fp ;
    char str[150],ch;
    int count=0;   
    
    fp = fopen("abc.c", "w") ;    //Create a new file & open in write mode.
    printf("Enter the characters for new file below size 150\n");

    while (strlen(gets(str))>0)
    {
        fputs(str, fp) ;   
        fputs("\n", fp) ;
    }    
    fclose(fp) ;
    
    rename("abc.c","simple.c");    
	   
   getch();       
}
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 ;
    char str[150],ch;
    int count=0;   
    
    fp = fopen("abc.c", "w") ;    //Create a new file & open in write mode.
    printf("Enter the characters for new file below size 150\n");
    while (strlen(gets(str))>0)
    {
        fputs(str, fp) ;   
        fputs("\n", fp) ;
    }    
    fclose(fp) ;
    
    remove("abc.c");
    printf("Created/Existing File is Deleted\n");
	 
    fp=fopen("abc.c","r");
    if(fp==NULL)
      {
        printf("File is not Present\n");      
      }	   
   getch();       
}
Example : WAP in C to count the number of characters present in a created/existing file.
#include<stdio.h>
#include<conio.h>

void main()
{
    FILE *fp;
    char sname[80], ch;
    int sage,count=0;
    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  = %.4f\n", fee);

    fclose(fp);
    
    printf("\nThe output of file is : \n\n");
    
    fp=fopen("student.txt","r");    
    while((ch=getc(fp))!=EOF)
	{
          printf("%c",ch);
	  count++;
        }	      
    fclose(fp) ;
    printf("\nTotal number of characters in the file is = %d\n",count);
    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[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)
	{        
	   putc(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();       
}
Example : How to convert lowercase letters of one file into uppercase letters in another file 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 to merge two file contents into one 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 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 : A file 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(c != 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.

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.