C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - General Questions (Q.No. 6)
6.
Which files will get closed through the fclose() in the following program?
#include<stdio.h>

int main()
{
    FILE *fs, *ft, *fp;
    fp = fopen("A.C", "r");
    fs = fopen("B.C", "r");
    ft = fopen("C.C", "r");
    fclose(fp, fs, ft);
    return 0;
}
"A.C" "B.C" "C.C"
"B.C" "C.C"
"A.C"
Error in fclose()
Answer: Option
Explanation:

Extra parameter in call to fclose().

Discussion:
27 comments Page 2 of 3.

Anu said:   1 decade ago
Please explain then how to print fp fs ft?

Vamsi Tharun Kumar said:   1 decade ago
Answer is 'D', Because comma operator used in the expression fclose (fp, fs, ft). The comma operator has left-right associativity. The left operator is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated.

Ashwini said:   1 decade ago
M getting segmentation fault even though m passing 1 parameter to it, can any 1 fix this problem?

Shilpa said:   1 decade ago
We can use fcloseall(); then all the opened files will be closed.

Bhanu Prakash said:   1 decade ago
The the correct option is C. "A.C"
there can be any no. of arguments passed to fclose.
But which file is closes depends on the functionality of the comma(,) operator. It takes the first argument.

Rocks said:   1 decade ago
@Bhanu prakash.

You telling that it takes the first argument. Simply you are making fool to c compilers because any compiler may be turbo c or gcc it read from the right to left. Any it leave the extra parameter.

Yogesh thuwal said:   1 decade ago
While fclose() read only one argument. Then we pass to the 3 files that time it will close 1 one.

Leenaja said:   1 decade ago
To close all the files opened with fopen()at a time, we can use file function closeall(). This function has no arguments.

Leenaja said:   1 decade ago
To close all the files opened with fopen()at a time, we can use file function closeall(). This function has no arguments.

Aakash said:   1 decade ago
fclose() can close only one file at a time. If more than 1 argument than it will take only first and ignore rest.


Post your comments here:

Your comments will be displayed after verification.