Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

419
Vistas
Is it necessary to close a file number of time it is opened in program?

If file is opened using fopen() and in different mode then is it necessary to close it number of time or it can be closed once at end of code ?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Each time you open the file, you are going to receive different file descriptors or file handles. So you have to close each file handle using fclose() if opened via fopen() or close() if opened via open().

over 4 years ago · Santiago Trujillo Denunciar

0

The open function is the underlying primitive for the fopen and freopen functions, that create streams.

int open (const char *filename, int flags[, mode_t mode])

which creates and returns a new file descriptor for the file named by filename. The argument mode is used only when a file is created, but it doesn’t hurt to supply the argument in any case.

So whenever an open or fopen called a new file descriptor is created and these descriptors stay allocated until the program ends.

The function close or fclose closes the file descriptor fields. Closing a file has the following consequences:

  • The file descriptor is de-allocated.
  • Any record locks owned by the process on the file are unlocked.
  • When all file descriptors associated with a pipe or FIFO have been closed, any unread data is discarded.

If you open or fopen and don't close, then that descriptor won't be cleaned up, and will persist until the program closes.

The problem will be severe if there are a lots of calls of open or any file can potentially be opened multiple times (without closing each time), then the descriptors will be leaked, until the OS either refuses or unable to create another descriptor, in which case the call to fopen fails and program can crash.

On most POSIX operating systems, including Linux and Os X, each process is allocated a fixed table of file handles, or file descriptors. This is typically about 1024 handles. When you try to open 1025th file descriptor the operating system kills your process.

The problem is almost certainly that you are leaking file handles. That is, handles are being opened, and after you are done with them they are not closed.

To avoid the descriptor leak, you must need to close or fclose before a file is reopened each time.

Take a look what GNU described about Opening and Closing Files.

over 4 years ago · Santiago Trujillo Denunciar

0

You have to close all of them.

When you use open-family syscalls, they return you a file-descriptor (fopen returns FILE* which is basically a struct that containts the file descriptor. Have a look on the FILE definition).

typedef struct 
{
 short level ;
 short token ;
 short bsize ;
 char fd ; <-- here
 unsigned flags ;
 unsigned char hold ;
 unsigned char *buffer ;
 unsigned char * curp ;
 unsigned istemp; 
}FILE ;

As a result fopen() will you give you two different FILE* (and different file-descriptors obviously) that you should close()/fclose() finally.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda