Posts

Showing posts with the label Basic File Operation

My Subject Blog

C Program for FILE Function fputc fputs fgets

/* Write a program to illustrate the use of fputc(), fputs(), and fgets(). */ #include<stdio.h> #include<string.h> int main()  {      FILE *fp;  char str[4];  char ch[4]={'a','b','c','d'};  int i;  fp=fopen("Test.txt","w");  if(fp == NULL)   {   perror("Error opening file");   return(-1);  }  for(i=0;i<4;i++)  { fputc(ch[i],fp); //use of fputc()  }  fputs("EFGH",fp);    //use of fputs()  fclose(fp);      fp=fopen("Test.txt","r");   if(fp == NULL)   {   perror("Error opening file");   return(-1);  }   while(fgets(str,4,fp)!=NULL)   //use of fgets()  { printf("%s",str);  }  fclose(fp);     return 0; } 

C Program for Basic File Operation

/* Write a program to create a file and perform basic I/O operations on the same.  */ #include<stdio.h> int main()  {     FILE *fp;     int ch;     fp=fopen("Test.txt","w");     if(fp == NULL)  { perror("Error opening file"); return(-1); } putc('A',fp);     fclose(fp);        fp=fopen("Test.txt","r"); if(fp == NULL)  { perror("Error opening file"); return(-1); }  ch=getc(fp);     printf("CHAR = %c",ch);     fclose(fp);     return 0; } 

Followers