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.

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.

R. rajkumar... said:   1 decade ago
Can we close some selected file at a time from the many no of files by use of fclose or any other method?

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.

Irresistiblegal said:   1 decade ago
The correct option id D coz..
syntax of fclose is-

#include <stdio.h>
int fclose( FILE *stream );

The function fclose() closes the given file stream, deallocating any buffers associated with that stream. fclose() returns 0 upon success, and EOF otherwise.

thats why when we give too much parameter inside fclose() it gives error-too many arguments to function 'fclose'.


Post your comments here:

Your comments will be displayed after verification.