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

245
Vistas
Linux/ Open directory as a file

I've been reading Brian Kernighan and Dennis Ritchie - The C Programming Language and chapter 8.6 is about directory listing under UNIX OS. They say that everything and even directory is a file. This means that I should be able to open directory as a file? I've tried it using stdio functions and it didn't work. Now, I'm trying it with UNIX system functions. Of course, I'm not using UNIX, I'm using Ubuntu linux. Here is my code:

#include <syscall.h>
#include <fcntl.h>

int main(int argn, char* argv[]) {
    int fd;
    if (argn!=1) fd=open(argv[1],O_RDONLY,0);
    else fd=open(".",O_RDONLY,0);
    if (fd==-1) return -1;

    char buf[1024];
    int n;
    while ((n=read(fd,buf,1024))>0)
        write(1,buf,n);

    close (fd);
    return 0;
}

This writes nothing even when argn is 1 (no parameters) and I'm trying to read current directory. Any ideas/explanations? :)

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

0

Files are also called regular files to distinguish them from special files.

Directory or not a regular file. The most common special file is the directory. The layout of a directory file is defined by the filesystem used.

So use opendir to open diretory.

over 4 years ago · Santiago Trujillo Denunciar

0

Nachiket's answer is correct (as indeed is sujin) but they don't clear up the mystery as to why open works and not read. Out of curiosity I made some changes to the given code to find out exactly what was going on.

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char* argv[]) {
    int fd = -1;
    if (argc!=1) fd=open(argv[1],O_RDONLY,0);
    else fd=open(".",O_RDONLY,0);
    if (fd < 0){
      perror("file open");
      printf("error on open = %d", errno);
      return -1;
    }
    printf("file descriptor is %d\n", fd);

    char buf[1024];
    int n;
    if ((n=read(fd,buf,1024))>0){
        write(1,buf,n);
    }
    else {
      printf("n = %d\n", n);
      if (n < 0) {
        printf("read failure %d\n", errno);
        perror("cannot read");
      }
    }
    close (fd);
    return 0;
}

The result of compiling and running this:

file descriptor is 3
n = -1
read failure 21
cannot read: Is a directory

That settles it, though I'd have expected open to fail, since the correct system function for opening directories is opendir().

over 4 years ago · Santiago Trujillo Denunciar

0

Though everything in unix is a file (directory also) but still filetype is concept is present in unix and applicable to all files. there are file types like regular file,directory etc and certain operations and functions are allowed/present for every file type.

In your case readdir is applicable for reading contents of directory.

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