Programming for Problem Solving (3110003)

BE | Semester-1   Winter-2019 | 07-01-2020

Q5) (a)

Write a program to illustrate the use of fputc ( ) and fputs( ).

Program

#include <stdio.h>
void main() {
    FILE *fp;
    char ch;

 //write a character to a file
    fp = fopen("file.c", "w");
    fputc('a', fp);
    fclose(fp);

 //read a character from a file
    fp = fopen("file.c", "r");
    while((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }
    fclose(fp);
    printf("\n");
}

Output:

a