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

限于喜欢 提交于 2019-12-08 01:42:38

问题


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?


回答1:


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.




回答2:


I found the Apache Portable Runtime Project (APR) which does a lot more than just file I/O and path operations. And is not tied to any project like Gnome.

Another project that has portable functions for this with a nice API is CZMQ with the zfile and zsys classes, although the library itself focuses on other things.




回答3:


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! :-)



来源:https://stackoverflow.com/questions/16355904/is-there-any-cross-platform-file-io-path-util-library-for-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!