File I/O by extension type

This is a discussion on File I/O by extension type within the C++ Programming forums, part of the General Programming Boards category; I am having difficulty reading in files that end in a specific extension. Essentially, I would like to open all ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    File I/O by extension type

    I am having difficulty reading in files that end in a specific extension. Essentially, I would like to open all of the files of a certain type within a folder that inludes files of various types. I am using dirent.h as my header file and (if it matters) my complier is Xcode. Any help, or, if possible, source code would be greatly appreciated. Thanks everyone!


    -romanpitch

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    So is your approach to read all the files, then match the extension?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    //Find First File
       HANDLE hFind;
        WIN32_FIND_DATA FindData;
        string file = "\\*.*";
        hFind = FindFirstFile ((file).c_str(), &FindData);
        doSomething(FindData.cFileName);
    
    // Look for more
    
        while (FindNextFile(hFind, &FindData))
        {
            doSomething(FindData.cFileName, );
        }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    I didn't think they wanted a Win32 answer mikeman118.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This FAQ tells how to read ALL files in a given directory [in teh fourth or so subsection]:
    http://faq.cprogramming.com/cgi-bin/...&id=1045780608

    From there, you need to use some "string pattern matching", e.g.
    Code:
    int hasExt(const char *name, const char *ext)
    {
        int nlen = strlen(name);
        int elen = strlen(ext);
        // Check first if there's a chance!
        if (nlen < elen) {
           return 0;
        }
        int index = nlen - elen;
        for(int i = 0; i < elen; i++) {
           if (elen[i] != name[index+i]) {
               return 0;
           }
        }
        // If we get here, it's a match!
        return 1;
    }
    This code is untested, and I'm sure somone will poke holes in it... But it should give you an idea of how to do it.

    Also, the above code is just checking if the file ends with "ext", so you could say
    Code:
    hasExt(name, "1.c");
    and it would match "foo1.c" and "blah1.c" for example.

    It's not strictly C++, as it doesn't use strings, but neither does readdir(), so I don't think that should be a requirement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about just using glob()? MacOS X provides that, doesn't it?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by Salem View Post
    I didn't think they wanted a Win32 answer mikeman118.
    I just figured that as long as it worked no one would care whether or not it was windows... unless they were using linux.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Xcode is the Mac OS X environment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21