Hello guys, I wasn't sure whether I should post this here or in a Linux forum. Forgive me if I'm wrong. The problem is the following: aren't Linux directories just regular files that have a list of other files as their contents? So why this dummy code works with a regular file but prints "error reading dir" with a directory?
Code:
char *file = "dir";
int fd;
if ((fd = open(file, O_RDONLY)) == -1)
printf("error opening %s\n", file);
char buffer[1024];
int n;
if((n = read(fd, &buffer, 1024)) == -1)
printf("error reading %s\n", file);
int i;
for (i = 0; i < n; ++i)
printf("%c", buffer[i]);
Perhaps directories are different from regular files in Linux? That would be strange. In Unix they are the same or so they are treated in the simple implementation of opendir, readdir, etc in K&R2. Any hints?
Thanks in advance for your help