-1

I want to get file extensions without using standart libraries in C. so basically i want to search for .txt files in a folder which includes .png, .txt, .jpg files.

I don't have a certain code to show, although while I was searching I found a code which includes <dirent.h> and everybody was saying this code cannot be done without that library. Can't I do it without that? and also in some websites they were saying <dirent.h> is only a library for Linux. I'm using MacOS.

code was this: How can I get only txt files from directory in c? error is this:

Member reference base type 'char [1024]' is not a structure or union.

Can you help me?

mousetail
  • 7,009
  • 4
  • 25
  • 45
zquab
  • 1
  • 1
  • 1
    _without using standard libraries in C_ you cannot traverse and get the folder tree. Darwin is also *nix based OS kernel. You _should_ be aware of the system being working on. – Soner from The Ottoman Empire Oct 11 '22 at 11:33
  • @SonerfromTheOttomanEmpire do you know which standart libraries i have to use? can you help me? is it ? – zquab Oct 11 '22 at 11:34
  • The extension is the part after the `.` so find the `.` and the extension is the part after that. – user253751 Oct 11 '22 at 11:35
  • 1
    Yes, the functions `opendir` and `readdir` from `` are what you want. – Steve Summit Oct 11 '22 at 11:36
  • dirent.h isn't a langauge-standard header; it's POSIX (which is available on your mac, so go nuts). – WhozCraig Oct 11 '22 at 11:37
  • @SteveSummit is only for Linux like they said? thank you for helping (: – zquab Oct 11 '22 at 11:38
  • @zquab firstly, it seems you are Turkish because in English standart is written as _standard_. Secondly, the website makes use of standard libraries and macos while doing this. https://iq.opengenus.org/traversing-folders-in-c/ – Soner from The Ottoman Empire Oct 11 '22 at 11:38
  • @zquab `` is from Unix and works fine on Unix, Linux, and MacOS. (A Windows programmer might have trouble using it, though.) – Steve Summit Oct 11 '22 at 11:39
  • @SonerfromTheOttomanEmpire yes that's right sorry for that haha. thank you, you really helped me a lot. – zquab Oct 11 '22 at 11:41
  • 1
    @user253751 well not exactly, in many file systems you can put dots in file names, so that won't always work. Moreover, on *nix systems hidden files start with a dot, but that doesn't make the part after that the extension :) – mikyll98 Oct 11 '22 at 11:43
  • `system("find . -name '*.txt'")` – pmg Oct 11 '22 at 11:43
  • @mikyll98 every filesystem that has extensions has them as the part after the dot in the filename. That's what a file extension is. If it does something different, it's not a file extension. – user253751 Oct 11 '22 at 11:46
  • @user253751 what about hidden files? ^^" – mikyll98 Oct 11 '22 at 11:47
  • @mikyll98 what about them? – user253751 Oct 11 '22 at 11:49
  • 1
    @user253751 is the part after the dot of a hidden file its extension? I don't think so :) – mikyll98 Oct 11 '22 at 11:50
  • 1
    @mikyll98 actually many tools will say yes! but if there is more than one dot, it's the part after the last dot – user253751 Oct 11 '22 at 11:57

1 Answers1

0

If it is supposed to work on macOS only, you can use macOS native frameworks instead of standard libraries, like this:

#include "CoreFoundation/CoreFoundation.h"

int main(int argc, const char * argv[]) {

    CFStringRef extension  = CFStringCreateWithCString(NULL, ".txt", CFStringGetSystemEncoding());
    CFStringRef folderURLString = CFStringCreateWithCString(NULL, "file:///Users/mousetail/Downloads/", CFStringGetSystemEncoding());
    CFURLRef folderURL = CFURLCreateWithString(NULL, folderURLString, NULL);
    
    CFURLEnumeratorRef enumerator = CFURLEnumeratorCreateForDirectoryURL(NULL, folderURL, kCFURLEnumeratorDefaultBehavior, NULL);
    while (true) {
        CFURLRef fileURL = NULL;
        CFURLEnumeratorResult enumeratorResult = CFURLEnumeratorGetNextURL(enumerator, &fileURL, NULL);
        if (kCFURLEnumeratorSuccess == enumeratorResult) {
            CFStringRef fileURLString = CFURLGetString(fileURL);
            if (CFStringHasSuffix(fileURLString, extension)) {
                CFShow(fileURLString);
            }
        } else if (kCFURLEnumeratorEnd == enumeratorResult) {
            break;
        }
    }
    
    CFRelease(enumerator);
    CFRelease(folderURL);
    CFRelease(folderURLString);
    CFRelease(extension);

    return 0;
}
Jiri Volejnik
  • 1,034
  • 6
  • 9