Worked, then made it a function: Console

This is a discussion on Worked, then made it a function: Console within the C++ Programming forums, part of the General Programming Boards category; Ok, i used demo code someone gave me in the main and it worked. It searched for mp3 files fine, ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Worked, then made it a function: Console

    Ok, i used demo code someone gave me in the main and it worked. It searched for mp3 files fine, and listed them as well as how many. Then i made the code its own function, and called that function in main. I know its something i did wrong, the code is below, and at the bottom of the code is the output commented off within the program. I have included all of the code, so u can see how i called it and whats going on.

    Code:
    /*	Steven Billington - not original author
    	December 10, 2002
    	Main.cpp
    
    	Program searches the given directory for any
    	file ending in the .mp3 extension. It will not
    	check to ensure these are genuine mp3 files, 
    	it will just report them as such based on the
    	files extension. If you save a text document 
    	with a .mp3 extension, for example, it will 
    	recognize this as a mp3 file in its search.
    
    	See readme for details.
    */
    
    /*	Our preprocessor Directives*/
    #include <iostream>
    #include <windows.h>
    #include <string>
    
    int find_files();
    
    /*	Define our STD's*/
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int main(int argc,char** argv)
    {
    
    	find_files();
    	return 0;
    	
    
    }
    
    int find_files()
    {
    	/*	Holds information found*/
    	WIN32_FIND_DATA wfd;
    
    	/*	Holds directory*/
    	string str;
    
    
    	/*	Counts files found*/
    	int i = 0;
    
    	/*	Prompt user for directory*/
    	cout << "Enter Directory: ";
    	cin>>str;
    
    	/*	Outout empty line*/
    	cout<<"\n";
    
    	/*	Indicates we want mp3 files*/
    	str += "\\*.mp3*";
    
    	/*	Find first file*/	
    	HANDLE hFile =  FindFirstFile(str.c_str(),&wfd);
    
    	/*	Aborts on error*/
    	if(hFile ==INVALID_HANDLE_VALUE)
    	{
    		cout << "Directory named " << wfd.cFileName << " found!";
    	
    		cout << "Error!! Aborting" << endl;
    		return 1;
    	}
    
    	do
    	{
    		/*	out files*/
    		cout<<wfd.cFileName<<endl;
    
    		/*	Increment counter*/
    		i++;	
    	}
    	
    	/*	Get the rest of the files*/
    	while(FindNextFile(hFile,&wfd));
    
    	/*	Output # of found files*/
    	cout<<"\nFound "<<i<<" Files.\n"<<endl;
    
    	/*	Clean up*/
    	FindClose(hFile);
    
    	
    	return 0;
    }
    
    /*
    
    OUPUT
    ______________________________
    
    
    
    Enter Directory: c:
    
    Directory named 
    
    
    _? found!Error!! Aborting
    Press any key to continue
    */

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The code worked ok for me ?? Now I got your output if there was no MP3 in the given directory for C: . But if I put a mp3 in C: it worked perfectly. Also if you get an invalid HFILE you should probably just say File Not Found or some such and output the string the user entered for the path it wasn't in. Good luck.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ooo someone took the mp3 file i was using for test off of the c (prolly me lol).

    thnx man.

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ya i fixed that, the whole if now just couts an error and returns one. Like i said i didn't write it originally i'm just imporiving it.

    As for the str line, its always empty now because i placed

    str = "";

    after its declaration, then made (for what type if file to look for) a new variable, ext, also a string.

    now the user inputs the type of file they want in format of *.ext* and hits enter.

    then it reads in that and adds \\ to it via

    ext = "\\" + ext;

    and finally the new str line is

    str += ext;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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