2012年11月4日 星期日

分辨副檔名 ( Distinguish Filename Extension)

strrchr : Locate last occurrence of character in string
              for example : filename is  hi_.mp3.txt
              its Filename Extension is .txt , not .mp3
              this is what I think to use "strrchr"
              你要找的字元,在該字串中最後一次出現的位置
              我故意取一個怪怪的檔名  hi_.mp3.txt   他的副檔名應該是.txt
              有兩種應用方法,如下所示:

char *dot_location;
char *filename = "hi_.mp3.txt" ;

dot_location = strrchr( filename , '.' );
printf ("Filename Extension = %s \n", dot_location  );
printf ("Last occurence of '.' found at %d \n", dot_location - filename + 1 );

 if ( (dot_location != NULL ) &&
                ((!strcmp( dot_location,".txt")) ||
                (!strcmp( dot_location,".doc"))  ||
                (!strcmp( dot_location,".pdf")) )
           ) 
            
        {
            printf ("document match!\n");
         }

======output=====================
Filename Extension = .txt
Last occurence of '.' found at 8
document match!

沒有留言:

張貼留言