Thread: Checking filetype

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Checking filetype

    I wish to create some code that checks if a string has the characters ".var" on the end of it or not.

    The idea is that the user enters the path and filename to locate a .var file. This is then stored in a string. If they enter another file type other than a .var, I would like to be able to detect this.

    I have attempted to use some string functions like strcspn but have not been able to come up with a viable solution, mainly because the string functions are not able to search strings with \ signs (vital part of a file path) in them without the compiler coming up with errors.

    Can anybody help?!

    Many thanks

    Paul

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try the strstr function.

    I have attempted to use some string functions like strcspn but have not been able to come up with a viable solution, mainly because the string functions are not able to search strings with \ signs (vital part of a file path) in them without the compiler coming up with errors.
    Huh? What are you trying? You probably need to escape the \ character, i.e. use \\ for each \ you want to use.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by paul_harris77 View Post
    I have attempted to use some string functions like strcspn but have not been able to come up with a viable solution, mainly because the string functions are not able to search strings with \ signs (vital part of a file path) in them without the compiler coming up with errors.
    Then either you are using a really bad compiler or there's something else going on. Notice also that this string
    Code:
    "\tell"
    does not contain a backslash character -- it contains a tab character, an 'e', two 'l's in a row, and then a null terminator. If you want a string (that you specify in your code) to have a backslash in it, you need to use the code for a backslash, which is \\ .

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hk_mp5kpdw View Post
    Try the strstr function.
    Could work, but it's not straightforward. If the filename is foo.var.blah then you'll find ".var" even though it doesn't end in ".var"

    I'd just check if the last four characters of the string are ".var":

    Code:
    int n = strlen( path );
    if( n >= 4 && strcmp( path + n - 4, ".var" ) == 0 )
        ...
    And the comment about backslashes not working makes no sense. ".var" contains no backslashes, so he must be referring to the string being searched. But if it's a literal string (which is the only reason there would be trouble with backslashes) why are you bothering to search it? You already know whether it ends in .var
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Thanks for the replies. The problem I had was when I added in the characters to search the string for. I put \ as one of these characters to search for and the compiler said that it was missing a terminating character. But this was probably the wrong way of going about it. I just need some way to do the following

    The user types in a path and filename which is entered into a string, but I wish to limit them to only typing in something like:

    C:\\Documents\blah.var

    instead of

    C:\\Documents\blah.jpg for example

    The file extension they type in must be .var

    Hence I need to search the string for the .var at the end of the string.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( (p = strrchr( s, '.' )) && strcmp( p, ".var" ) == 0 )
        printf( "Yay, file extension!\n" );
    else
        printf( "Fail.\n" );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM