3

What's the best way to work with file systems in C?

There is of course dirent.h , but from what I've heard it isn't completely guaranteed to be available on all platforms and compilers, for example the Microsoft Visual C++ compiler, for one, doesn't support it.

For now, the best thing I've seen is the file handling functions in GLib. Is there any better way?

What I am looking for is a well-designed, preferably cross-platform library / code / something else supported by all main-stream compilers and is well-documented. Any ideas?

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 1
    Why C? Could you possibly pick a worse language? – Puppy Mar 02 '12 at 18:54
  • What specifically do you want to do with file systems? If you simply want to read/write files, stdio.h will do the job. But I think you want to do more, such as traverse directories etc. Is that correct? – Adam Mihalcin Mar 02 '12 at 18:58
  • 1
    since file system work is inherently system dependent, cross platform libraries will be few and far between – KevinDTimm Mar 02 '12 at 19:00
  • @DeadMG I'm not going to get into a flame war, but I'd like to hear your explanation of why C is so bad. After all, your favorite operating system kernel (whether it be Windows, or Mac OS, or Linux) is written mostly in C. – Adam Mihalcin Mar 02 '12 at 19:00
  • @AdamMihalcin well, to be specific, I want to open a directory, search all files in it for a '*.lmap' extension, and parse them. – ApprenticeHacker Mar 02 '12 at 19:00
  • 2
    @AdamMihalcin: Horrendously unsafe and insecure? Incredibly slow? No generics? No resource management? No namespaces? No exceptions? Horrific macro abuse? That's just to begin with, *and* that's just the bad things about C that C++ doesn't share, let alone an exhaustive list. Those kernels are written in C because there was nothing else at the time, not because C is a good idea. It isn't. Also, if you don't wish to start a flamewar, come to the C++ chat and I will gladly enumerate for you in *great detail* exactly how C sucks. – Puppy Mar 02 '12 at 19:03
  • @AdamMihalcin [link](http://chat.stackoverflow.com/rooms/10/loungec) to the above mentioned chat room. FIGHT! FIGHT! FIGHT! :D – ApprenticeHacker Mar 02 '12 at 19:05
  • 1
    @DeadMG: C is slow? i thought that was the one thing it was good at. – Claudiu Mar 02 '12 at 19:24
  • 1
    @Claudiu: Tell that to O(n) string length, for example. Or no inlinable algorithms? There are other examples. – Puppy Mar 02 '12 at 19:33
  • I second @DeadMG here. C has strengths for *a few* use cases (interop with other languages or embedded programming for instance), but certainly not for manipulating files in a system agnostic way... Did you look into languages like Python (if C is not an absolute requirement) ? – Alexandre C. Mar 02 '12 at 20:28
  • @DeadMG So much for civility and politeness. – Adam Mihalcin Mar 02 '12 at 22:14
  • Thanks all, actually there was a problem with a C Library which had variables with names like `class` which I couldn't use it with C++. But now its solved with the help of @DeadMG. Now I finally can use something better than C. – ApprenticeHacker Mar 03 '12 at 05:26

1 Answers1

2

I recently posted an answer about cross-platform socket libraries, but it's equally good for your question:

Community
  • 1
  • 1
Christoph
  • 164,997
  • 36
  • 182
  • 240