Subscribe: RSS
  • Hi Jamie and others at Datalight support,

    Thanks for all your support so far on our project.

    I am wondering about the most efficient way to find if a file exists in a particular directory?

    Some Googling has revealed a few POSIX possibilities, namely:

    access(): Unfortunately, this is not supported by the Reliance Edge as far as I can see.
    red_open(): attempt to open a file with RED_O_CREATE, this fails if the file already exists... This is nice only if you want to create a file that does not already exist... But what if you just need to figure out if a file exists?
    red_fstat(): This nearly does what I need, but it takes the iFilDes argument instead of the name of the file. I think this means it will only give the status of an open file. I don't think the similar POSIX stat() function is available with Relaince Edge.

    So, I am concluding that the only way to know if a file exists (given its name) is to use red_opendir(), red_readdir(), red_closedir() to find the file. This does not seem that efficient... Is there a better way?

    Many thanks,

    • Our engineers have suggested that you consider using red_open() without the RED_O_CREAT flag? If the file exists, it succeeds; if the file does not exist, it fails with the RED_ENOENT errno.
      • Thanks,

        That has done the job.... My code includes a layer above the POSIX style API that gives a "C" like file interface, I have found that setting the C permission on fopen() to "r+" has the effect of removing "RED_O_CREAT" from the flags in the "red_open()" API call.

        1 question, what would happen if the file is already open and I call red_open()?

        Many thanks,

        • Paul,

          If the file is already open, opening it again would open another independent file descriptor. If that file descriptor is unneeded, it can be closed (which wouldn't affect other file descriptors for the same file).
          • Many thanks,

            That's cleared things up.