fcloseall() function : Closing All Open Streams in C Programming

Closing all the Open Streams in C Programming :

Sometimes we open multiple files for processing then it is tedious task to close all the open streams one by one after usage. C provides powerful feature to close all the open streams using single method.

fcloseall() : Does not closed following Streams

1 stdin Standard Input Stream
2 stdout Standard Output Stream
3 stdprn Standard Printer Stream
4 stderr Standard Error Stream
4 stdaux

Declaration :

int fcloseall(void);

Remark : Explanation

Remark Point Explanation
Return Value on Success No of Streams Closed by Function
Return Value on Failure EOF
Usage Closing all the open streams

Program : Live Example

#include<stdio.h>
{ 
int streams_closed;
fopen("ONE.txt","w");
fopen("TWO.txt","w");
streams_closed = fcloseall();
if (streams_closed == EOF)
    printf("Error");
else 
    printf("%d Streams Were Closed", streams_closed);
return 0;
}

Output :

2 Streams Were Closed

Explanation :

We have opened two files in write mode so that we have two streams open.

fopen("ONE.txt","w");
fopen("TWO.txt","w");

Now we are closing all the streams using single statement.Function will return no of streams closed by function

streams_closed = fcloseall();

Return value is compared with the EOF, If return value is not EOF then we can say that function has successfully closed open streams

if (streams_closed == EOF)
    printf("Error");
else
    printf("%d Streams Were Closed", streams_closed);