C Programming - Input / Output - Discussion


Point out the correct statements about the program?


int main()
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
            fputs(str, fptr);
            fputs("\n", fptr);
    return 0;

[A]. The code copies the content of one file to another
[B]. The code writes strings that are read from the keyboard into a file.
[C]. The code reads a file
[D]. None of above

Answer: Option B


This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.

Piyush Agarwal said: (Aug 21, 2014)  
Without <string. H> how is this program even working?

Shivani said: (Jul 31, 2020)  
Shouldn't this run for infinite time because of the while loop? Please, anyone, tell me.

Pawan Kumar Singh said: (Jul 9, 2022)  
But without string.h how it possible sir?
It shows an error, right?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.