Getting file extension in C language -
Say that the file is named 12345.jpg. In C, how can I get the file extension so that I can compare it to some file extensions? If there is any inbuilt functions, please let me know.
A function to do this, together with a test harness:
< Code> #include & lt; Stdio.h & gt; #include & lt; String.h & gt; Const char * getExt (const char * fspec) {char * e = strrchr (fspec, '.'); If (E == faucet) E = ""; // fast method, can use more (FSPEC [Stellen (FSPEC)]). Return e; } Int main (int argc, char * argv []) {int i; For (i = 1; i & lt; argc; i ++) {printf ("[% s] -> [% s] \ n", argv [i], getExt (argv [i])); } Return 0; }
is running with:
./program abc abc. Abc.1 .xyz abc.def abc.def.ghi
gives you:
[abc] - & gt; [] [ABC.] - & gt; [.] [ABC.1] - & gt; [.1] [.xyz] - & gt; [.xyz] [abc.def] - & gt; [.def] [abc.def.ghi] - & gt; [.ghi]
Comments
Post a Comment