Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any cross platform file IO / path util library for C?

Is there some C library that implements platform independent version of path operations?

Some examples that come to mind:

  • Getting the absolute path from a relative one (without requiring the file to exist)
  • Ensuring a directory exists.
  • Creating a file if non-existent, and any directory in the given path that is missing.
  • Concatenating paths.
  • Getting directory part of path.
  • Getting filename part of path.
  • Lots more....

There are of course platform specific versions of these, but they hardly ever do exactly the same thing leading to chaos trying to match behaviour across platforms.

And I mean C, not C++, so boost doesn't count.

Also if there is none, are there any good sources/examples of differences between platforms, and other useful information that can be used to write such a library?

like image 918
Joakim Avatar asked Oct 22 '25 05:10

Joakim


2 Answers

I know this is a rather old question, but I have been looking for something like this and couldn't find anything. So I wrote one, cwalk (reference) - for anyone who still finds it useful! :-)

like image 89
likle Avatar answered Oct 23 '25 20:10

likle


The ever-nice glib family (part of GTK+) has some, at least.

  • The misc utility functions API has functions like g_path_is_absolute() and others
  • The I/O virtualization wrapper API GIO has g_file_make_directory_with_parents().

Note that some of the functions you're describing, such as "getting absolute path from relative one" assumes the existence of a current directory, which is how that resolution is typically done. It's not a very "pure" function, since it relies on external state. In Windows, with its "split file system", there is even one current directory per volume (e.g. one for C:\, one for A:\ if you have it, and so on) per process.

like image 42
unwind Avatar answered Oct 23 '25 22:10

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!