Thread: Filename matches string input----How to implement in C?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Filename matches string input----How to implement in C?

    Hello Group! I have an assigment question as follows..

    Problem:
    In Windows, we could use wildcard characters (* and ?) while searching for a file or folder.
    * means there could be 0 or more characters replacing it
    ? means there could be exactly one character replacing it
    Given a search string and a file name, your C function should say whether the file name matches the search string or not.


    My Friends said that this function must be someway similar to 'grep' function in unix-shell programming.Other that that,i dont even have the least idea of how to do this.


    I'd like to do every bit of this by myself,but i could do with some direction..Could anyone help me with where to start or what material to refer to?

    Thanks:-)

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take a look at our C Tutorials. All the information you need to know for this project can be found there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is a regular expression library you could use, but that would probably be considered cheating. Think about it this way: everything that isn't a * represents a fixed-length portion of your file name that must match. So splitting on the * first might be a good idea. If there is no ?, it must match exactly, so something like strncmp or strstr might help out. If there is a ? you may want to "split" your string again into "exact match" parts and "any char" parts, or you may want to write your own strncmp_with_question_mark that checks for string equality using the special "? matches anything" rule. If you do split on * and ?, be wary, because strtok does not work when being used on two different strings at once (it's thread-safe brother strtok_r does, however).

    That should get you going for the bulk of cases. You may need some special consideration for fixed-length portions of the string that start with a ? though, e.g. "abc*?def".

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks bro:-)...That was really helpful:-)

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Raja211991 View Post
    Hello Group! I have an assigment question as follows..

    Problem:
    In Windows, we could use wildcard characters (* and ?) while searching for a file or folder.
    * means there could be 0 or more characters replacing it
    ? means there could be exactly one character replacing it
    Given a search string and a file name, your C function should say whether the file name matches the search string or not.
    This is a huge task...
    Think of the possibilities you have to look for... using a simple filename like... abcd.txt
    Any of the following examples (and a whole lot more!) would have to match it.

    *d.t??
    ??cd.*
    a*d.?x?
    *c*.t*
    ?b*.t??

    and on and on and on...

    You're not going to do this in 20 lines, I can promise you that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I Cant input filename and its annoying
    By rogster001 in forum C Programming
    Replies: 6
    Last Post: 08-18-2009, 11:45 AM
  2. File input/output, using a string as a filename?
    By Helgso in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2008, 03:13 AM
  3. input filename to open a file
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-10-2007, 08:15 AM
  4. Replies: 6
    Last Post: 04-11-2006, 04:52 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM