Thread: file filter extension in c

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    file filter extension in c

    How i can create file filter extension in c. For example i want the fopen function works only with files that ends with .txt extension

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Then you have to write a function to test if the file extension entered by the user is part of a list of acceptable extensions... and depending on the return value of the function either open the file or tell the user you're not going to.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have the include file string.h listed in your program.

    Have a small char array for the extension. They if the filename you get has a period in it, put the letters after the period, into another small char array. Terminate it with a '\0' char, so it's a legit string.

    Now use strcmp(). If ((result=strcmp(array1, array2))==0) then it's a match.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <string.h>
    #define TYPES ".txt.ini.bak.asc"
    
    int IsApprovedType(char *str)
      { if (strstr(str,TYPES))
          return 1;
         return 0; }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    That is. until someone types in
    mybadfile.txt.exe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good point Salem. Be sure to only use letters for you extension string, which are the last letters in the filename.

    Might as well just collect them right from the back of the filename, and decrement the index, until you reach the first dot:

    filename.txt.exe
    Rough code idea:
    Code:
    //your program already has the filename in it's array
    char txt[5]; //for text file extensions+end of string char +1
    
    if(strchr(filename,'.') {
       i=strlen(filename);
       j=0;
       txt[0]='\0';
       while(filename[i] !='.') {
          txt[j]=filename[i];
          ++j;
          --i;
       }
    }
    
    //now do your strcmp() with txt[]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a File Extension?
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2006, 12:45 PM
  2. .ef file extension
    By xddxogm3 in forum Tech Board
    Replies: 8
    Last Post: 02-08-2006, 06:20 AM
  3. What file extension should I use?
    By f0r3nsic in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2005, 01:13 PM
  4. allow another file extension??
    By C+noob in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2005, 08:52 PM
  5. get file extension
    By Nada in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 09:12 AM

Tags for this Thread