C Programming
Computer Programming

C file handling program to write text into file and read character by character from the file



Write C file handling program to write text into file and read character by character from the file

\* C file handling program to write text into file and read character by character from the file *\

# include < stdio.h >
# include < stdlib.h >
int   main( )
{

/* file pointer*/
FILE *fp ;
char fName[20] ;

printf("\n Enter file name to create :") ;
scanf( "%s",fName ) ;

/*creating (open) a file*/
fp = fopen(fName, "w") ;

/*check file created or not*/
if( fp == NULL )
{
      printf(" File does not created!!!") ;
      exit(0) ;
}
printf(" File created successfully.") ;

/*writting into file*/
putc('A',fp) ;
putc('B',fp) ;
putc('C',fp) ;
printf("\n Data written successfully.") ;
fclose(fp) ;

/*again open file to read data*/
fp=fopen(fName, "r") ;
if( fp == NULL )
{
      printf("\n Can't open file!!!") ;
      exit(0) ;
}

printf("\n\n Contents of file is :\n ") ;
printf("%c", getc(fp)) ;
printf("%c", getc(fp)) ;
printf("%c", getc(fp)) ;
fclose(fp) ;
return 0;

}

Output of Program :