Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. File Processing
  5. File Processing Operation...
  6. Write one character to file

Write one character to file

  • Writes one character to a file.
  • Function fputc receives as arguments a character to be written and a pointer for the file to which the character will be written.
  • The function call fputc(‘a’, stdout) writes the character ‘a’ to stdout— the standard output.
  • This call is equivalent to putchar(‘a’).

Syntax

fputc
int fputc(int char, FILE *pointer)

Program

#include<stdio.h>
int main()
{
    int i = 0;
    FILE *fp = fopen("output.txt","w");
     // Return if could not open file
    if (fp == NULL)
      return 0;
 
    char string[] = "Listen Class";
    for (i = 0; string[i]!='\0'; i++)
   {
         // Input string into the file
        // single character at a time
        fputc(string[i], fp);
     }
    fclose(fp);
    printf("Open the file output.txt");
    return 0;
}

Output

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments