Thread: Filename validation

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Filename validation

    Hopefully a simple question (searching MSDN came up with some clear answers but only for Windows Forms and the .NET class framework, neither of which I'm using, just the plain C Win32 API here)

    How do I find out if a string is a valid filename? (i.e. it doesn't contain bad characters and has a proper length)

    Thanks in advance!
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The length is fairly easy. Looking for invalid characters is much harder, as many characters are valid in certain places, but not in other places. Of course, the OS will check that the filename IS valid before it opens the file, so opening a file for reading will automatically check if it is valid. The value in GetLastError() or errno will be different depending on the cause of failure, so you can tell if the file didn't exist but has a valid name, or if the filename itself is invalid (e.g contains invalid characters, is too long). It may be harder to tell the difference between an invalid path and a file that doesn't exist - do you need to do this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    The path isn't an issue as I'm only saving files in the program's working folder.
    Problem is I want to be able to check whether it's a valid filename long before the file is actually created.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Boksha View Post
    The path isn't an issue as I'm only saving files in the program's working folder.
    Problem is I want to be able to check whether it's a valid filename long before the file is actually created.
    Yes, so try to open the file for reading - if that succeeds, close it, if it doesn't succeed, then check the "error code" to see if it failed due to the file missing (ENOENT), the the file isn't there (but implied that it's a valid name). If it's some other error, then it's not there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You could have a look at PathCleanupSpec, but you can only be sure you can create a file by creating it (you may not have permission, a network drive may be offline, etc).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Replies: 3
    Last Post: 01-25-2006, 10:30 PM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM