Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda