adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C to write and read name in/from file

  • Program in C to write name in file



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 int main()
  {
   FILE *file;
   char name[20];
   file=fopen("Test.txt","w");
   if(file==NULL) //if(!file) //if(file==0) 
     {
      printf("Error in creating file!.");
      exit(0);
   }
 printf("Enter your name:-");
 gets(name);
 fputs(name,file);
 close(file);
 return 0;
  }
Output:-




  • Program in C to read name from file



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 int main()
  {
   FILE *file;
   char name[20];
   file=fopen("Test.txt","r");
   if(file==NULL) //if(!file) //if(file==0) 
     {
      printf("File is not found.");
      exit(0);
   }
 printf("Your given name :-");
 fgets(name,20,file);
 printf("%s",name);
 close(file);
 return 0;
  }
Output:-








NOTE 
   Write mode : If file is not available in the folder where source file is available then compiler create file same name as the given file's name in the fopen() function.

Read mode : If file is not available in the folder where source file is available then fopen() function
returns 0 value (means file is not found ). So file reading Process is not performed

Related Programs


  1. Program in C to write and read text in/from file Using fgetc()/fputc() functions
  2. Program in C to write and read data in/from file Using fprintf()/fscanf() functions
  3. Program in C to write and read data in/from file Using fwrite()/fread() functions
  4. Program in C to write and read integer data in/from file Using getw()/putw() functions
  5. Program in C to copy the contents of one file to another file.
  6. Program in C to read the text file and count the number of vowels and consonants present in the file.
  7. Program in C to read the text file and count the number of words present in file
  8. Program in C to write text in file append mode
  9. Program in C to write and read number in/from file Using fprintf()/fscanf() functions
  10. Program in C to write and read number in/from file and find largest and smallest number present in the file.

Post a Comment

0 Comments