fprintf in c code example

Example 1: printf in c

#include <stdio.h>

int printf(const char *format, ...);

int main(void)
{
  int nb = 20;
 
  printf("Hello World !\n");
  printf("%d\n", nb);
  printf("%s/%d\n", "Nice", 20);
  return (0);
}

/// output :
///
///	Hello World !
///	20
///	Nice/20
///

Example 2: what is fprintf in c

#include<stdio.h>
int main() {
   int i, x = 4;
   char s[20];
   FILE *f = fopen("new.txt", "w");
   if (f == NULL) {
      printf("Could not open file");
      return 0;
   }
   for (i=0; i<x; i++) {
      puts("Enter text");
      gets(s);
      fprintf(f,"%d.%s\n", i, s);
   }
   fclose(f);
   return 0;
}

Example 3: fputs in c

#include <stdio.h>

int main () {
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1) {
      c = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

Example 4: what is fprintf in c

int fprintf(FILE *fptr, const char *str, ...);