fclose in c

If you want to know more on fclose in c then read this post.

What is fclose in c programming?

fclose in c is used to close the file which was opened for a specific purpose.

It is a file handling function in c.

fclose in c is found in standard input/output library i.e. stdio.h.

Upon closing the file using fclose all the buffers are also flushed.

Prototype


int fclose(FILE *fp);

Parameters of fclose in c

The fclose function closes the file which is pointed by a file pointer.

Return value of fclose in c

It returns zero after successfull operation otherwise EOF is returned as error.


Example programs for fclose in c

Example 1

#include<stdio.h>

int main()
{
  FILE *fpointer;
  int i;
  fpointer=fopen("scholar.txt","w");
  for(i=1;i<=3;i++)
     putw(i,fpointer);
  fclose(fpointer);
  return 0;
}

Code explanation

The file is closed by calling fclose function after putting values into the file.

Example 2

#include <stdio.h>

int main () {
   FILE *fpointer;
 
   fpointer = fopen("scholar.txt", "w");

   fprintf(fpointer, "%s", "Hello,this is scholarsoul");
   fclose(fpointer);
   
   return(0);
}

Code explanation

The file is closed by calling fclose function after putting values into the file.

fclose() Man page


Difference between fopen and fclose in c.

fopen in c used to open a file to perform a task like reading/writing. fclose in c is used to close a file that is opened to perform a task.

fopen in c returns a file pointer otherwise NULL on error. fclose in c returns zero after successful operation otherwise EOF is returned as an error.

fopen works on a file in reading/writing mode. fclose only closes the file which was opened in reading/writing mode.

fopen uses the buffer. fclose flushes the buffer.

Prototypes are as follows

FILE *fopen(const char *fname,const char *mode);

int fclose(FILE *fp);
fopen in cfclose in c
Opens a file in read/write mode.Closes the file.
FILE *fopen(const char *fnmae,const char *mode);int fclose(FILE *fp);
Returns a file pointer otherwise NULL on error.Returns zero after successfull operation otherwise EOF is returned as error.
Uses the buffer.Flushes the buffer.

Difference between fseek and fclose in c.

fseek in c used to change the file pointer position to a specified stream. fclose is used to close the opened file.

fseek returns zero on successful operation otherwise non-zero value on error. fclose returns zero after successful operation otherwise EOF is returned as an error.

fseek takes three arguments. fclose takes only one argument.

fseek seeks in an open file. fclose closes the opened file.

Prototype is as follows

int fseek(FILE *strm,long int offst,int whnce);

int fclose(FILE *fp);
fseek in cfclose in c
Used to change the file pointer position to a specified stream.Used to close the opened file.
int fseek(FILE *strm,long int offst,int whnce);int fclose(FILE *fp);
Returns zero on successful operation otherwise non-zero value on error.Returns zero after successfull operation otherwise EOF is returned as error.
Takes three arguments.Takes only one argument.
Seeks in an open file.Closes the opened file.

Difference between ftell and fclose in c.

ftell in c is used to get the total size of a file. fclose is used to close a file.

ftell returns the current value of a file pointer. fclose returns zero after successful operation otherwise EOF is returned as an error.

ftell function works on open file. fclose function closes the open file.

Prototypes are as follows

long int ftell(FILE *stm);

int fclose(FILE *fp);
ftell in cfclose in c
Used to get the total size of a file.Used to close a file.
long int ftell(FILE *stm);int fclose(FILE *fp);
Returns the current value of a file pointer.Returns zero after successfull operation otherwise EOF is returned as error.
Works on open file.Closes the open file.

Difference between fprintf and fclose in c.

fprintf function in c is used to format the output to a file instead of stdout console. fclose closes a file.

fprintf returns the number of characters written. if any error occurs it returns a negative value(-1). fclose returns zero after successful operation otherwise EOF is returned as an error.

fprintf works on open file. fclose closes a open file.

fprintf takes many arguments. fclose takes only one argument.

Prototypes are as follows

int fprintf(FILE *file_pointer, const  char *format_string,.......);

int fclose(FILE *fp);
fprintf in cfclose in c
Used to format the output to a file instead of stdout console.Closes a file.
int fprintf(FILE *file_pointer, const char *format_string,.......);int fclose(FILE *fp);
Returns the number of characters written. if any error occurs it returns negative value(-1).Returns zero after successful operation otherwise EOF is returned as error.
Takes many argumentsTakes only one argument.

People also ask for

What is the difference between fscanf and fclose in c?

fscanf is used to read formatted data from a file. fclose functions as closing a file.

How does buffer is manipulated while fclose works?

fclose causes buffers to be emptied. Buffers that are allocated by the standard i/o are freed.

What is the difference between free and fclose function?

free function deallocates the memory. fclose flushes buffers.

What happens if fclose not called?

If fclose is not called operating system will close the file when the program completes its operation. But if the case of the file is used as an output file then the data may be buffered and not written to a file.

Does fclose function fails?

Yes it may fail and sets errorno to one of the following errors
1.EAGAIN
2.EBADF
3.EFBIG
4.EINTR
5.EIO
6.ENOSPC
7.EPIPE

What does Fclose do in C?

fclose in c is used to close the file which is opened for a specific purpose.

What does Fclose return?

It returns zero after successfull operation otherwise EOF is returned as error.

Can Fclose fail?

Yes it may fail and sets errorno to one of the following errors
1.EAGAIN
2.EBADF
3.EFBIG
4.EINTR
5.EIO
6.ENOSPC
7.EPIPE

How many arguments are in Fclose function?

One argument is passed to the fclose function that is file pointer.

Does Fclose free memory?

fclose cleans uu resources allocated by fopen.