C Program File Handling example

Top C Program to File Handling (File I/O)

C Program to File Handling (File I/O)

C Program to File Input/output.

C Program to creating, opening and closing a text file.

#include<stdio.h>

#include<conio.h>

 void main()        

{

 FILE *filep;

filep = fopen("file.txt","w");

fclose(filep);

  getch();

}

 

C Program to open and write a text file.

 

#include <stdio.h>

#include <conio.h> 

 void  main()

{

   FILE *fptr;

 if (fp = fopen("Resume.txt", "wb"))

{

 printf("File opened successfully and write in binary mode .");

   }

   else

   printf("Error!");

   fclose(fptr);

   getch();

}

 

C Program to open and write to a text file using fprintf.

#include <stdio.h>

#include <conio.h> 

#include <stdlib.h>

 void  main()

{

   int number;

   FILE *fileptr;

fileptr = fopen("D:\\program.txt","w");

   if( fileptr == NULL)

   {

      printf("Error in file !");  

      exit(1);            

   }

printf("Enter number: ");

scanf("%d",&number);

fprintf( "fileptr %d",number);

   fclose( fileptr );

    getch();

   }

 

C Program to open and write to a text file using fprintf.

# include <stdio.h>

#include <conio.h> 

# include <string.h>

 void main( )

{

   FILE *fptr ; 

char data[100] = “Welcome to Code Hub-A Computer Science Portal for Engineering UG and PG students";

 fptr = fopen("program.c", "w") ;

 if(fptr == NULL )

    {

printf( "program.c file failed to open." ) ;

    }    

else

    { 

printf("The file is now opened\n") ;

  if (strlen(data) > 0 )  

      { 

fputs(data,  fptr ) ;   

  fputs("\n",  fptr ) ;

        }    

fclose( fptr ) ;

printf("Data successfully written in file \n");

printf("The file is closed.") ;

    }

    getch();

  }

 

C Program to Open for reading in binary mode

 

#include <stdio.h>

#include <conio.h> 

 void  main()

{  

FILE * fileptr;  

if(fileptr= fopen("add.txt", "rb"))

{  

printf("File is successfully opened. ");    

}  

else    

printf("The file is not availabe! And cannot create a new file using binary mode");  

 fclose( fileptr );  

   getch();

  }

 

C Program to Open for writing in binary mode.

#include <stdio.h>

#include <conio.h> 

 void main()

 {  

 FILE * fptr;  

 if ( fptr  = fopen("resume.txt", "wb"))

{      

printf("File is successfully open and write in binary mode");  

  }  

 else  

  printf("Error!");    

 fclose( fptr );  

    getch();

  }

 

C Program to file open in append mode.

 

#include <stdio.h>

#include <conio.h> 

   void  main()

 {    

 FILE *  fptr ;  

 if (file = fopen("resume.txt", "a"))

{      

printf("File is successfully open in append mode");  

  }  

 else  

  printf("Error in file!");

      fclose( fptr );  

  getch();

}

 

C Program to Open for both reading and writing mode.

#include <stdio.h>

#include <conio.h> 

   void  main()

{  

 FILE * fptr;

if(fptr=fopen("resume.txt", "r+"))

{    

  printf("File is successfully open in read and write mode");  

   }

 else  

 printf ("The file is not available! And cannot create a new file ");  

   fclose( fptr );    

  getch();

} 

C Program to Open for append in binary mode.

 

#include <stdio.h>

#include <conio.h> 

   void  main()

{    

FILE * fileptr;  

 if (file = fopen("resume.txt", "ab"))

{  

 printf("File is successfully open in append mode");  

 }    

else  

 printf ("Error!");  

 fclose(file);  

  getch();

}

 

C Program to Open for both reading and writing in binary mode.

#include <stdio.h>

#include <conio.h> 

   void  main()

{  

  FILE * fileptr;

  if (fileptr = fopen("program.txt", "rb+"))

{      

printf("File is successfully open in reading mode");  

 }    

else    

printf("The file is not available! And cannot create a new file ");  

   fclose(file);

    getch();

  }

 

C Program to Open for both reading and writing.

#include <stdio.h>

#include <conio.h> 

 void  main()

{    

FILE * fileptr;  

 if (fileptr = fopen("pogram.txt", "w+"))

{    

 printf("File is opened successfully open in read and write mode ");  

 }

    else  

  printf("Error message!");  

   fclose(fileptr);    

getch();

}

 

C Program to Open for both reading and writing in binary mode.

#include <stdio.h>

#include <conio.h> 

   void  main()

{

   FILE * fileptr;  

   if (file = fopen("Resume.txt", "wb+"))

{    

   printf("File is opened successfully open in read and write ");  

 }  

 else  

 printf("Error message!");  

   fclose(fileptr);  

   getch();

 }

 

C Program to Open for both reading and appending.

#include <stdio.h>

#include <conio.h> 

 void  main()

{  

 FILE * fileptr;  

 

  if (fileptr = fopen("Resume.txt", "a+"))

{    

 printf("File is successfully open.");  

  }  

 else    

printf("Error message !");

   fclose(fileptr);  

  getch();

 }

 

C Program to Open for both reading and appending in binary mode.

#include <stdio.h>

#include <conio.h> 

   void  main()

 {  

 FILE * fileptr;

 if (file = fopen("resume.txt", "ab+"))

{      

 printf("File is successfully open in read-append mode ");    

}  

 else    

printf("Error message!”);

  fclose(fileptr);

    getch();

  }

 

C Program to File Handling using fgets function.

 

#include <stdio.h>

#include <conio.h> 

   void  main()

 {  

   FILE * fileptr;  

   char string[200];

 if (fileptr = fopen("resume.txt", "r"))

 {    

printf("%s", fgets(string, 50, fileptr));  

  }    

 fclose(fileptr);    

     getch();

  }

 

C Program to File Handling using fgetc function.

#include <stdio.h>

#include <conio.h> 

   void  main()

{  

  FILE * fileptr; char string;  

  if (fileptr = fopen("resume.txt", "r"))

{    

  while((string=fgetc(fileptr))!=EOF)

      printf("%c",string);

   }    

fclose(fileptr);  

  getch();

 }

 

C Program to File Handling using fputs function.

 

#include <stdio.h>

#include <conio.h> 

   void main()

{  

 FILE * fileptr;  

 if (file = fopen("resume.txt", "w"))

{  

if(fputs("Code Hub", fileptr) >= 0)    

printf("Content written to the file successfully.");    

}

   fclose(fileptr);    

getch();

}

 

C Program to File Handling using putc function.

 

#include <stdio.h>

#include <conio.h> 

 void  main()

 {      

FILE * fileptr;  

 if(fileptr=fopen("resume.txt", "w"))

{    

  fputc('How Are You !',  fileptr );  

 }

    fclose( fileptr );

     getch();

 }

C Program to storing employee information using file handling.

#include <stdio.h>  

#include <conio.h> 

 

void  main() 

    FILE *fileptr; 

    int emp_id; 

    char emp_name[30]; 

    float total_salary; 

fileptr = fopen("emp.txt", "w+");

if ( fileptr == NULL) 

    { 

printf("File does not available in computer system\n"); 

      return; 

    } 

printf("Enter the emp_id\n"); 

scanf("%d", &emp_id); 

fprintf(fptr, "Emp Id= %d\n", id);

printf("Enter the employee name \n"); 

scanf("%s", emp_name); 

fprintf(fptr, "Employee Name= %s\n", emp_name); 

printf("Enter the Total Salary\n"); 

scanf("%f", &total_salary); 

fprintf(fptr, "Total Salary= %.2f\n", total_salary); 

fclose(fptr); 

getch();

}

 

 

Comments

Popular posts from this blog

C Program to print swap number using third variables

C Program to open and write to a text file using fprintf.