Q:

How to use fputc in C programming

belongs to collection: File handling in C

0

The fputc() function writes the character (unsigned char) to the output stream, at the specified position (indicated by the associated file position indicator) and then advances the indicator appropriately. It takes two arguments integer and files stream. The integer value is internally converted to an unsigned char.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In the below code, I am writing A-Z in the newly created file (aticleworld.txt)  using the fputc function.

#include <stdio.h>
int main()
{
    int ch = 0;
    FILE *fp = NULL;
    //create a file
    fp = fopen("aticleworld.txt", "w");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Write A to Z in file
    for(ch =65 ; ch <= 90 ; ++ch)
    {
        fputc(ch, fp);
    }
    //close the file
    fclose(fp);
    printf("A t0 Z written to the created file\n");
    return 0;
}

Output:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to use fgetc in C Programming... >>
<< Reading an array of structure using fread in C...