Thread: Folder access library

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    18

    Folder access library

    I have a file with about 200-300 images and I want to perform some functions on them all but I don’t want to have to go to the trouble of having to type out
    CvLoadImage(“img1.jpg”); run program than edit it to CvLoadImage(“img2.jpg”); run program ect.
    There are no order to the names of the files there called random things.

    Basically I want my program to access the folder they will be in, select each image one at a time, and run it once for the lot of them.
    What function or libry should I look at to help with this?

    Is there one that can count the amount of files in a folder and select there names and pop it into a variable so I can do something along the lines like this
    Code:
    Loop till end of folder{
    a=name of file.jpg;
    CvLoadImage(a);
    }
    Last edited by aprop; 11-07-2010 at 03:34 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Use the right tool for the right job; use a shell script in this case.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In C++, the approach to do this is specific to the operating system.

    If doing this under windows, look up the functions FindFirstFile() and FindNextFile() in the win32 API.

    Under most (I'd hesitate to say all) flavours of unix, look up the functions findfirst() and findnext().

    You might also try the boost filesystem library (look up the directory_iterator class). There are trade-offs with using that library, but it does aim for portability - and is/was the basis for a proposal to include such functionality in a future C++ standard (I'm not sure of the status of that, offhand - I seem to recall some concerns with an "illusion of portability" in discussion).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Have a read the FAQ for files and directory handling.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or from the command line

    myprog.exe *.jpg

    Then munch your way through the argv[] of your main, processing each file.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    Or from the command line

    myprog.exe *.jpg

    Then munch your way through the argv[] of your main, processing each file.
    That only works with a command shell that expands wild cards.

    It does not, for example, work with the windows command line - myprog.exe will receive "*.jpg" as argv[1].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you link it with setargv, it will.
    Expanding Wildcard Arguments

    TBH, that took a while to find. I'd forgotten just how badly M$ command line sucks even after 30+ years.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    I'd forgotten just how badly M$ command line sucks even after 30+ years.
    It is easy to forgive one who manages to forget the incomprehensible

    Although, if you respect its limits, the MS command line program is not bad. Of course, those limits test your ability to show respect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    18
    Hey thanks for the feedback.
    Im working in windows 7 using visual studio 2010.
    Quote Originally Posted by MWAAAHAAA View Post
    Use the right tool for the right job; use a shell script in this case.
    Quote Originally Posted by ssharish2005 View Post
    Have a read the FAQ for files and directory handling.

    ssharish
    Im using windows from what I read about Shell scripts and from what I read in the faq Im not sure if they apply, unistd wouldn’t work in my #include for example.
    I could be wrong though if I am let me know .

    Quote Originally Posted by grumpy View Post
    In C++, the approach to do this is specific to the operating system.

    If doing this under windows, look up the functions FindFirstFile() and FindNextFile() in the win32 API.

    Under most (I'd hesitate to say all) flavours of unix, look up the functions findfirst() and findnext().

    You might also try the boost filesystem library (look up the directory_iterator class). There are trade-offs with using that library, but it does aim for portability - and is/was the basis for a proposal to include such functionality in a future C++ standard (I'm not sure of the status of that, offhand - I seem to recall some concerns with an "illusion of portability" in discussion).
    This is where most of my attention has been the last 2 days trying to use these functions with opencv.
    Now I can pull all the files in my folder but they are not feeding into my cvLoadImage function.
    Can anyone see what I may be doing wrong?
    Code:
    void findf()
    {
    	
    	//directory varibles
    	LPCTSTR lpFileName;
    	HANDLE hfind1;
    	TCHAR hj;
    	//opencv image
    	IplImage* Image;
    	//string and char array
    	string name;
    	char name1[100];
    	//counters
    	int i=0;
    	int q=0;
    	 //not my real directory but my path name is correct, and pulling the bmp files
    	 lpFileName=TEXT("c:/mydir"); 	 
    	
    WIN32_FIND_DATA FileData;
    hfind1=FindFirstFile(lpFileName,&FileData);
    
    do{		
    		q++;
    		//print out file name
    		_tprintf(FileData.cFileName);
    		cout<<" "<<q<<"\n ";
    //this puts the cFileName into a char array name1
    do {
                                    name1[i]=(1,FileData.cFileName[i]);
                                    //up i
                                    i++;
    
    }
    
    while(FileData.cFileName[i]!=0);
    
    //once we have out name1, bring i to 0 once more
    cout<<name1<<" \n";
    
    //here images are inputted ,but not been displayed
    Image=cvLoadImage(name1);
    cvShowImage("new",Image);
    
    
    // release the image
    cvReleaseImage(&Image);
    
    //return i to 0 and clear the name1 array
    for(i;i!=-1;i--)
    {
    //clear name1 array
    name1[i]=0;
    
    }
    
    
    }
    //find the next file
    while(FindNextFile(hfind1,&FileData)!=0);
    
    //close the hfind
    FindClose(hfind1);
    
    }
    Also on another note I noticed 2 files called
    . and ..
    are these just back tracks to the folder before hand, i managed to discard them using the wild card operator *?
    Last edited by aprop; 11-10-2010 at 10:21 AM. Reason: typo

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, . is the current directory, and .. is the parent directory.

    And is that the best job you could manage on the indentation front?
    SourceForge.net: Indentation - cpwiki
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create an Import Library in Vs2008
    By khumayun in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2010, 10:49 AM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Library Wrapper
    By cusavior in forum C Programming
    Replies: 3
    Last Post: 03-25-2008, 10:27 AM
  4. Difficulty choosing graphics library
    By jdiperla in forum Game Programming
    Replies: 11
    Last Post: 02-27-2008, 06:35 PM
  5. PHP CVS System?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-24-2004, 05:29 PM