• Advertisement

Archived

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

read file extension

This topic is 5784 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
OMG, i just went for some coffee.. 4 reply''s allready! Thanx dudes!

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
quote:
Original post by Xces
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?

Who cares? Good habits come through good practice. I try to keep as much of my code conforming to the standard as possible, and I''m sure that I''ve made myself a better programmer because of it. By "C++ 7" I''m assuming you mean "MSVC 7", just keep in mind that it''s not the only compiler around.

Share this post


Link to post
Share on other sites
Ok, you got a point there, and indeed i mean MSVC7. So you prefer the implementation of the char for char lowercasing, or do you have another idea for that?

Regards

Xces

Share this post


Link to post
Share on other sites
quote:
Original post by Xces
So you prefer the implementation of the char for char lowercasing, or do you have another idea for that?

I''d prefer it over relying on a potentially non-existant strlwr function. You can always just write the loop version as your own strlwr function (you might not want to name it that, that''s up to you). I''m should post my ''GetFileExtension'' function as long as I''m replying (it''s in C, but it''s easy to C++-ify it, just change found_period to a bool, 0 to false, and 1 to true):

  
const char *GetExtension(const char *filepath) {
int found_period = 0;

for( ; *filepath != ''\0''; ++filepath) {
if(*filepath == ''.'') {
found_period = 1;
break;
}
}
if(found_period == 0) return NULL;

while(*filepath != ''\0'') ++filepath;
while(*filepath != ''.'') --filepath;

return &filepath[1];
}

Since it basically just returns an offset to the data after the last period (or NULL if there is no period) it doesn''t have any limitations with extension length. It doesn''t copy the extension into a new string because of this.

Share this post


Link to post
Share on other sites
Ok, lets say i have this (c++) code:
char picname[255];
char *the_ext;

(and picname contains "thefile.jpg").
How would i use your code? so the_ext contains the extension?
Sorry for being a newbie

Share this post


Link to post
Share on other sites

char picname[255] = "thefile.jpg";
char *the_ext = GetExtension(picname);

After the call to GetExtension, the_ext points to picname[8], which is basically the string "jpg".

Share this post


Link to post
Share on other sites

  • Advertisement