read file extension

Started by
14 comments, last by Xces 22 years ago
hey guys, i am reading a file wich looks like this: -------- Items: 4 001.jpg 002.jpg 003.jpg 004.jpg -------- But when i read the picname, i want to extract the file extension. (e.g. bmp, jpg, tiff, psd) How do i read the file extension? I am searching for an implementation which lowercases the string, and then searches for the extension of the file. Greetz Xces [edited by - Xces on April 20, 2002 2:02:11 PM]
Advertisement
I''m sure there is a better way to do it then what I am going to show you, but I just don''t know that way yet.


  char *filename = "Something.jpg";char extension [4];int length;length = strlen(filename);extension[0] = filename[length-3];extension[1] = filename[length-2];extension[2] = filename[length-1];extension[3] = ''\0'';  


I think that would work, or atleast give you a push in the right direction.
"...."
The above ought to work. Just make sure all the extensions are 3 chars long.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Include the period and use strstr(filename,".jpg"). That''ll work if ".jpg" isn''t anywhere else in the string. Even if it is, strstr returns the position the string was found at, or NULL if it wasn''t found.
To put it in lower case just do

  for(int ct=0; ct<strlen(str); ct++)str[ct]=tolower(str[ct]);   


remember to include stdlib.h(cstdlib) or ctype.h(cctype)



[edited by - gregs on April 20, 2002 2:45:00 PM]
OMG, i just went for some coffee.. 4 reply''s allready! Thanx dudes!
try strlwr(filename) first. It''s easier than lowering each character.

------------------------------
Baldur K
"Hey! I hate these Microsoft guys! What a rotten compiler! It only accepts 16,384 local variables in a function!"
Ok, i think i found another way of doing it ...


// Extension searchchar	*pdest;int	result;int	dot=''.'';// Convert to lowercasestrlwr(picname);// Find last occurence of the "."pdest = strrchr( picname, dot );result = pdest - picname + 1; 


My idea is to copy all from the . to the end of the string to read the file extension.
How to continue?
quote:Original post by baldurk
try strlwr(filename) first. It''s easier than lowering each character.

The strlwr function is not standard, and many compilers don''t include it.

true true, but i am making my app in C++ 7, and it only has to run on my computer. Any suggestions on that code above?

This topic is closed to new replies.

Advertisement