file handling in c




 file handling using c programming:-


 example 1 :- write to a text file-

  #include<stdio.h>

  #include<conio.h>

  #include<stdlib.h>

     int main() {

  FILE *fp;

  int num;

          fp=fopen("files1.1.txt","w");

  if(fp==NULL) {

printf(" file does not exit");

exit(1);

}

printf("enter the num:");

scanf("%d",&num);

fprintf(fp,"%d",num);

printf("\n value of n is :%d",num);

fclose(fp);

return 0;

}

   

 example 2:- read to a text file-

 

   #include<stdio.h>

   #include<conio.h>

   #include<stdlib.h>

int main() {

FILE *fp;

int num;

      //if((fp=fopen("files1.1.txt","r"))=NULL)

        fp=fopen("files1.1.txt","r");

if(fp==NULL) {

printf(" file does not exit");

exit(1);

}

fscanf(fp,"%d",&num);

printf("\n value of n=%d",num);

fclose(fp);

return 0;

}

example 3:- in file wite a character-

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

 int main()

 {

 FILE *fp;

 char ch;

 fp=fopen("f1.txt","a");

  if(fp==NULL)

   {

    printf("file does not exist!!!");

    exit(1);

  printf("enter the character:");

  scanf("%c",&ch);

  fprintf(fp,"%c",ch);

  printf("\n ur character is %c",ch);

 

return 0;

 }


example 4:- in file read a character-


#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

 int main()

 {

 FILE *fp;

 char ch;

 fp=fopen("f1.txt","r");

  if(fp==NULL)

   {

    printf("file does not exist!!!");

    exit(1);

  

  fscanf(fp,"%c",&ch);

  printf("\n ur character is: %c",ch);

 fclose(fp);

return 0;

 }


example 5:-in file read a string-

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 int main()
 {
 FILE *fp;
 char ch[50];
 fp=fopen("f1.txt","r");
  if(fp==NULL)
   {
    printf("file does not exist!!!");
    exit(1);
  
  fscanf(fp,"%s",&ch);
  printf("\n ur character is: %s",ch);
 fclose(fp);
return 0;
 }

example 6:- in file write a string-

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 int main()
 {
 FILE *fp;
 char ch[50];
 fp=fopen("f1.txt","a");
  if(fp==NULL)
   {
    printf("file does not exist!!!");
    exit(1);
  printf("enter the character:");
  gets(ch); 
               // used for read  full string also gets() allow th users to                         enter the space separated string
    fprintf(fp,"%c",ch);
 puts(ch);   
return 0;
 }


Comments

Popular posts from this blog

Practical slips programs : Machine Learning

Full Stack Developement Practical Slips Programs

Android App Developement Practicals Programs