read file extension

Started by
14 comments, last by Xces 21 years, 12 months ago
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.

Advertisement
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
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.

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

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".

Cool, thanx

This topic is closed to new replies.

Advertisement