-
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
- Program in C to write and read text in/from file Using fgetc()/fputc() functions
- Program in C to write and read data in/from file Using fprintf()/fscanf() functions
- Program in C to write and read data in/from file Using fwrite()/fread() functions
- Program in C to write and read integer data in/from file Using getw()/putw() functions
- Program in C to copy the contents of one file to another file.
- Program in C to read the text file and count the number of vowels and consonants present in the file.
- Program in C to read the text file and count the number of words present in file
- Program in C to write text in file append mode
- Program in C to write and read number in/from file Using fprintf()/fscanf() functions
- Program in C to write and read number in/from file and find largest and smallest number present in the file.
0 Comments