How to use fputc in C

How to use fputc in C programming?

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.

Syntax of fputc in C:

int fputc(int c, FILE *stream);

 

Return value of fputc():

If fputc has written the character successfully, then return the written character. If there is any error, then it returns EOF.

 

Example code of fputc in C,

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:

fputc in c

 

Code Analysis:

In the above c fputc example, first, I have opened or created the text file (“aticleworld.txt”) in writing mode and get the file pointer. Using the if condition, I am verifying that file has been opened or created successfully or not.

fp = fopen("aticleworld.txt", "w");
if(fp == NULL)
{
    printf("Error in creating the file\n");
    exit(1);
}

 

After creating the file successfully, I have used for loop to write the value from 65 to 90 in the file.

for(ch =65 ; ch <= 90 ; ++ch)
{
    fputc(ch, fp);
}

 

In the last, I have used fclose to close the created file.

fclose(fp);

You can check this Article.

If the stream is opened with append modes, the fputc function appended the character to the end of the output stream. Let see an example, in which I am opening the above-created file (aticleworld.txt) in append mode and writing 1 using the fputc function.

#include <stdio.h>

int main()
{
    //Ascii value of 1
    int ch = 49;
    FILE *fp = NULL;

    //Open file in append mode
    fp = fopen("aticleworld.txt", "a");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Write 1 in file
    fputc(ch, fp);

    //close the file
    fclose(fp);

    printf("Append 1 to the created file\n");

    return 0;
}

 

Output:

fputc Append mode

 

putc Vs fputc in C:

The putc function is equivalent to fputc but putc could be implemented as a macro. According to C standard “The putc function is equivalent to fputc, except that if it is implemented as a macro, it may evaluate stream more than once, so that argument should never be an expression with side effects”.

Using of putc is risking because it is implemented a macro, so there might be a side effect that occurs due to the macro.  It is good to use fputc in place of putc.

Recommended Articles for you: