Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

read file extension

This topic is 5875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites
Ok, i think i found another way of doing it ...



// Extension search
char *pdest;
int result;
int dot=''.'';

// Convert to lowercase
strlwr(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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!