Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1
    Join Date
    Jan 2007
    Posts
    50

    loading all of the files inside of a specific folder?

    I'm making a small app in sdl that loads all the mp3 files found in a specific folder called soundtrack. However, I don't know how to load all of the files in the folder without knowing the files name already. I want to be able to put in mp3 file in this folder, and have my program load it, along with all the others.

    I've never been very good at FILE IO, it's always been a shortcoming of mine.

    does anyone know where I can learn how to do this?

    I'm also having problems with file parsing in general with c++ (like loading a simple config file).

  2. #2
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    k, well. First things first, have you successfully obtained a list of files?
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  3. #3
    Join Date
    Jan 2004
    Posts
    76
    Hi, heres what I came up with after a quick search on google :

    Code:
    #include "stdafx.h"
    #include <io.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	_finddata_t fileinfo;
    	
    	long h_file =_findfirst("C:\\YourFolder\\*.*", &fileinfo);
    	_findnext(h_file, &fileinfo);
    	_findnext(h_file, &fileinfo);
    
    
    	_findclose(h_file);
    	
    
    
    	return 0;
    }
    _findfirst fills the fileinfo wich contains the file name and returns a handle used in the call of the _findnext function.
    I don't know why but it returns no valid file info for the first two calls of the findfirst/findnext functions when I don't specify a file type (like *.bmp instead of *.*) in _findfirst but it still works.
    Last edited by DA_HUI; 11-27-2008 at 04:30 PM.

  4. #4
    Join Date
    Jan 2007
    Posts
    50
    Quote Originally Posted by DA_HUI View Post
    Hi, heres what I came up with after a quick search on google :

    Code:
    #include "stdafx.h"
    #include <io.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	_finddata_t fileinfo;
    	
    	long h_file =_findfirst("C:\\YourFolder\\*.*", &fileinfo);
    	_findnext(h_file, &fileinfo);
    	_findnext(h_file, &fileinfo);
    
    
    	_findclose(h_file);
    	
    
    
    	return 0;
    }
    _findfirst fills the fileinfo wich contains the file name and returns a handle used in the call of the _findnext function.
    I don't know why but it returns no valid file info for the first two calls of the findfirst/findnext functions when I don't specify a file type (like *.bmp instead of *.*) in _findfirst but it still works.
    oh sorry, when I meant I/O I didn't know there was a specific library called that. what I meant was standard I/O in c++ (std stuff).

    I wouldn't know how to obtain a list of files, thats how little I know about it.

  5. #5
    Join Date
    Jun 2005
    Location
    NY
    Posts
    1,374
    If you are only concerned with Windows you can use FindFirstFile and FindNextFile.
    CodeGuru: DLL Tutorial For Beginners by me. Rated 4 1/2 out of 5.

  6. #6
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Okay, well first things first - the directory functions are not a part of the C++ language, they're a part of the included libraries.

    If you want to get a list of files in a specific folder, first you call the _findfirst function, then you continue to call the _findnext function. From memory, you continue to call this function until it returns 0 or NULL, or something like that to indicate there are no more files to be found.

    So, run the _findnext function inside a loop, breaking from the loop when there are no more files to be found. Whether you use a vector to stor the found filenames, or do the loop twice - once to count the number of files and allocate enough memory to hold them all, and a second time to actually read the filenames.

    The vector would seem the far, far better choice. There was some code here from dodlegion that showed use of vectors for a similar purpose. It may prove helpfull for you.

    Simon.
    Last edited by enhzflep; 11-28-2008 at 02:16 AM. Reason: misspelt fellow user's name

  7. #7
    Join Date
    Jan 2007
    Posts
    50
    Quote Originally Posted by enhzflep View Post
    Okay, well first things first - the directory functions are not a part of the C++ language, they're a part of the included libraries.

    If you want to get a list of files in a specific folder, first you call the _findfirst function, then you continue to call the _findnext function. From memory, you continue to call this function until it returns 0 or NULL, or something like that to indicate there are no more files to be found.

    So, run the _findnext function inside a loop, breaking from the loop when there are no more files to be found. Whether you use a vector to stor the found filenames, or do the loop twice - once to count the number of files and allocate enough memory to hold them all, and a second time to actually read the filenames.

    The vector would seem the far, far better choice. There was some code here from Dodelion that showed use of vectors for a similar purpose. It may prove helpfull for you.

    Simon.
    haha, I thought about this for a bit, and I realized that Linux uses a completely different file structure from windows, so obviously they would have to have different implementations. I don't know why I didn't think of this earlier.

    With that library, it seems easy enough to implement (especially with your explanation).

    I would like to bring up that I'm doing this to play the Mp3s in the folder, so wouldn't it be better to load one at a time instead of them all? but would doing that fragment the memory?

  8. #8
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Sorry for the wait, just had a few things that needed attending to.

    I realized that Linux uses a completely different file structure from windows, so obviously they would have to have different implementations.
    Yeah kinda, but not really. NotSoSuperHero mentioned the names of the Windows specific functions (as defined in windows.h or thereabouts), while the solution provided thus far uses libraries available to both windows and linux hosts. The precise under-the-covers implementation will be different, though you need not concern yourself with this, as _findnext and _findfirst and _findclose are all a part of the standard C library and thus are available regardless of the platform.


    I would like to bring up that I'm doing this to play the Mp3s in the folder, so wouldn't it be better to load one at a time instead of them all? but would doing that fragment the memory?
    Guess that really depends on how many files there are, and what their combined size is, as to whether it's worth pre-loading them all. All the code does so far, is to retrive a list of all the file names. I would probably just load each file as it is needed, sucking it's name back out of the list of filenames I'd created.

    Here's some code that will seach for and list (as well as count) all mp3 files in the folder the executable is run from (the curent directory. To use a specific folder, just insert the full path - remember to use double \'s. I.e c:\\mp3s\\*.mp3 rather than c:\mp3s\*.mp3 On linux, of course this would be /mp3s/*.mp3 or wherever the location is - double /'s aren't needed in linux. It's just that the \ character is an escape character, this is why we need two of them in filepaths under windows)

    Code:
    #include <io.h>
    #include <stdio.h>
    #include <string.h>
    
    void listMp3Files()
    {
        struct _finddata_t c_file;
        long hFile;
        int numFiles = 0;
        char *fileMask = "*.mp3";
    
        if ( (hFile = _findfirst(fileMask, &c_file)) == -1L )
            printf("No %s files in current directory\n", fileMask);
        else
        {
            do
                {
                    numFiles++;
                    printf("%s\n", c_file.name);
                }
            while ( _findnext(hFile, &c_file) == 0 );
            _findclose(hFile);
            printf("Number of %s files: %d\n", fileMask, numFiles);
        }
    }
    
    int main(int argc, char* argv[])
    {
        listMp3Files();
        return 0;
    }

  9. #9
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    imho, the whole _findfirst()/_findnext() approach is quite a mess for c++.

    if you don't mind the additional dependency, you should check out the boost::filesystem library. not only will this allow linux compatibility, but it also gets rid of the ugly c-style function based api.

    here is a short example of how to list a directory:
    Code:
    #include <stdexcept>
    #define BOOST_FILESYSTEM_NO_DEPRECATED
    #include <boost/filesystem.hpp>
    #include <iostream>
    
    void ls(boost::filesystem::path const& path) {
    	boost::filesystem::directory_iterator end_it;
    	for(boost::filesystem::directory_iterator it(path);
    		it != end_it;
    		++it) {
    			if( boost::filesystem::is_directory(it->status()) ) {
    				std::cout << " d ";
    			} else {
    				std::cout << " - ";
    			}
    			std::cout << it->path() << std::endl;
    	}
    }
    
    int main() {
    	ls("./");
    	return 0;
    }
    Last edited by ComicSansMS; 11-28-2008 at 02:12 AM.

  10. #10
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Yeah, I know some people hate to mix c with c++ code. I think it's a perfectly valid aproach myself, as it encourages a programmer to think about and know some of the internals of code that happens to be included in c++ libraries.
    The thing that immediately comes to mind is linked lists. They're a great thing to cut your teeth on, I reckon. Mmmm-mmm doubly-linked circular-lists, sounds like fun...

    Besides, how big's your exe file Comic? Mine's 6kb....

    That said, this difference in exe size only makes a huge difference with small programs. But still, it's worth keeping in mind.

    S.
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •