Thread: Problem with While Loop

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    6

    Problem with While Loop

    I'm trying to get my code to go through every file in a directory and check to see if it contains an ID3 tag or not. If it has no ID3 tag it's supposed to say so. My problem is that on most directories it just makes it through 2 files and stops. Or if I point it at a directory that only contains other directories then it says no ID3 tag found twice and stops. But if I point it to my /home/bl0w directory it lists it for every single file. I can't seem to figure out why when I point it to a directory with no mp3s it works, but when I point it to a directory with mp3s, it makes it look like there is only 2 files which aren't mp3. Any help would be greatly appreciated.

    Code:
    	//Open Directory For Reading
    	DIR* dir = opendir(argv[1]);
    	if (!dir) {perror("opendir");}
    	struct dirent* entry;
    
    	//For Each File In Directory...
    	while ((entry = readdir(dir)) != NULL) 
    	{
    		char buffer[256];
    		//Open File For Reading
    		if((file = fopen(entry->d_name, "r"))==NULL) return(1);
    
    		//Move To Last 128 bytes
    		fseek(file,-128L,SEEK_END);
    
    		//Read To Buffer and Close File
    
    		fread(buffer,1,128,file);
    
      		fclose(file);
    
    		//Look For 'TAG', Indicating ID3 Tag
    
    		if (strncmp(buffer,"TAG",3)==0)
    
        		{
    
    			char *b=buffer+3,*c=b+29;
    
        			//Read out title & artist
    
    			while(c>b && *c==' ') c--;
    
    			*++c=0;
    
    			//Save Title
    
    			strcpy(song,b);
    
    			b=buffer+33; c=b+29;
    
    			while(c>b && *c==' ') c--;
    
    			*++c=0;
    
    			//Save Artist
    
    			strcpy(artist,b);
    
    			//Save Year
    
    			if ((year=atoi(buffer+93))==0) year=9999;
    			printf(song);
    			
    			
    
    		}
    
    		else
    
    		{
    
    			printf("File Not MP3 or No ID3 Tag exists.\n");
    		}
    		}
    
    	
    	//Close Directory
    	if (closedir(dir) == -1) {
        		perror("closedir");
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if((file = fopen(entry->d_name, "r"))==NULL) return(1);
    1. You don't test for directories before opening files.
    2. You return without doing closedir()
    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.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    That statement is after I open the directory, isn't it? Maybe I don't understand it right.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( (file = fopen( blah, blah )) == NULL )
    {
        closeyourdirectory( );
        return now;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    I made those changes and it's still doing it. When I point it to /home/bl0w I get alot of "No ID3 Tag Exists", just like it's supposed to. But when I point it to /home/bl0w/Music I only get 2 "No ID3 Tag Exists". /home/bl0w/Music only contains other directories, no files at all.

    I don't get it. For some reason when it sees no files, it goes through the While loop twice before quiting everytime.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suggest you get this working before doing anything else
    Code:
    while ((entry = readdir(dir)) != NULL) 
    {
        printf( "Found entry = %s\n", entry->d_name );
        file = fopen ( entry->d_name, "r");
        if ( file != NULL ) {
          /* success, quietly close it again */
          fclose( file );
        } else {
          perror("Failed to open for reading" );
        }
    }
    Until you get a useful list of filenames printed, don't bother with anything else.

    Then when you do have a list, write a separate function to deal with a single file, rather than having one super-massive function to do everything.


    Check this FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    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.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Ahhh, thanks. I'll give that a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM