Write Integers to a Text File using the function fprintf() - C File

C examples for File:Text File

Description

Write Integers to a Text File using the function fprintf()

Demo Code

#include <stdio.h>

int main()/*  w  w  w.jav  a  2  s .  c om*/
{
     int i, k = 0;
     FILE *fptr;
     fptr = fopen("C:\\Code\\numbers.dat", "w");
     if (fptr != NULL) {
          puts("File numbers.dat is opened successfully.");
          for(i = 0; i < 10; i++)
              fprintf(fptr, "%d ", i+1);
          puts("Data written to file numbers.dat successfully.");
          k = fclose(fptr);
          if(k == -1)
              puts("File-closing failed");
          if(k == 0)
              puts("File is closed successfully.");
     }
     else
         puts("File-opening failed");
     return(0);
}

Result


Related Tutorials