1

I am trying to solve an exercise for my university. In this matter, how can I print the index of a directory with the type of each file, and the date of the last modified file, if the input is the name of the directory?

I was trying to use something like that (dir->d_name & R_OK) but I cant cause expression must have integral type

#include <stdio.h>
#include <dirent.h>

void properties(struct stat file);

int main()
{
    DIR *d;
    struct dirent *dir;

    char location[500]


    printf("Write the file location: \n");
    scanf("%s", location);
    d = opendir(location);

    //Emfanisi index tou directory
    if (d != NULL)
    {
        while ((dir = readdir(d)) != NULL)
        {
            printf("%s\n", dir->d_name);
            printf("\nType: ");
            
            if (dir->d_name & R_OK)
        }

        closedir(d);
    }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
manolex
  • 11
  • 4
  • 3
    Welcome to StackOverflow! Please read https://stackoverflow.com/help/how-to-ask. Also worth to read: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – harper Jan 04 '22 at 10:50
  • 1
    This task doesn't make any sense without a specific OS in mind. There is no way to do this with standard C since it doesn't really know about files, only file streams. – Lundin Jan 04 '22 at 10:52
  • You can show your attempt. You can use [listing files in dir](https://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program) and [man 2 stat](https://linux.die.net/man/2/stat) as a reference to write this program. – Shubham Jan 04 '22 at 10:53
  • Also this is probably more convenient to do in bat/bash/etc scripts than in C, because C involves dragging in some relatively complex OS-specific API. – Lundin Jan 04 '22 at 10:55
  • I am trying to solve it the last 2 hours but I am stacked cause stat and dirent are incompatible. So I cant use st.mode. – manolex Jan 04 '22 at 10:58
  • If you have some code you should add it to your question using the [edit](https://stackoverflow.com/posts/70577558/edit) facility: people are far more likely to provide help if they see the effort already made on your part. – G.M. Jan 04 '22 at 11:01
  • Just added it. Might be very wrong – manolex Jan 04 '22 at 11:03
  • You need to call [`stat`](https://man7.org/linux/man-pages/man2/lstat.2.html) for each file. – Jabberwocky Jan 04 '22 at 11:09
  • @Jabberwocky Could you write the command? I dont know how to call each file! – manolex Jan 04 '22 at 11:13
  • 2
    First Remove `printf("\nType: "); if (dir->d_name & R_OK)` from your code. This code prints all file names. Then for each of these file names you need to call `stat` (or `lstat`) and display the information in the stat structure. At the end of the [`stat`](https://man7.org/linux/man-pages/man2/lstat.2.html) documentation there is an example that shows how to use `stat`. Combine your code and the example. – Jabberwocky Jan 04 '22 at 11:18

0 Answers0