How do you get a file extension (like .tiff
) from a filename in C?
Thanks!
A CAM file is a CAM (computer-aided manufacturing) data file saved in the FastCAM format. It contains CAD (computer-aided design) information, which includes the processing path and material, thickness, and quantity details of the object. CAM files are similar to . DXF files developed by Autodesk.
MS-DOS and Windows command line In MS-DOS, typing dir to list all files also displays the file extension of each file.
const char *get_filename_ext(const char *filename) { const char *dot = strrchr(filename, '.'); if(!dot || dot == filename) return ""; return dot + 1; } printf("%s\n", get_filename_ext("test.tiff")); printf("%s\n", get_filename_ext("test.blah.tiff")); printf("%s\n", get_filename_ext("test.")); printf("%s\n", get_filename_ext("test")); printf("%s\n", get_filename_ext("..."));
Find the last dot with strrchr
, then advance 1 char
#include <stdio.h> /* printf */ #include <string.h> /* strrchr */ ext = strrchr(filename, '.'); if (!ext) { /* no extension */ } else { printf("extension is %s\n", ext + 1); }
You can use the strrchr
function, which searches for the last occurrence of a character in a string, to find the final dot. From there, you can read off the rest of the string as the extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With