Answer to Question #302811 in C for Suvedhaa

Question #302811

Write a C program for File Operations to read a string of characters from standard input and prints the entered string of charatcters using fgetc() and fputc() function. fgetc() is used to obtain input from a file single character at a time. fputc() is used to write a single character at a time to a given file.


Input : good bye


Output : good bye

1
Expert's answer
2022-02-25T17:39:04-0500
#include <stdio.h>


int main() {
    int buf[1024];
    int i, n;


    for (i=0; i<1024; i++) {
        int ch;
        ch = fgetc(stdin);
        if (ch == EOF) {
             n = i+1;
            break;
        }
        buf[i] = ch;
    }
    fputc('\n', stdout);
    for (i=0; i<n; i++) {
        fputc(buf[i], stdout);
    }
    fputc('\n', stdout);


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS