Is there a ready function to check if the string contains a valid file name? C ++ language.

2 answers 2

In .NET, I would use the GetInvalidPathChars function and then scan the line for their presence. In WinAPI, PathFileExists is the most suitable

And let's define the following points:

  1. What OS are we working on? It is clear that under Linux you can easily create files with names like COM1, from which Windows can tear down the roof.
  2. What file systems do we use? On fat12, it is potentially impossible to create files with names other than 8.3. And there are limitations on the number of characters on other filesystems.
  3. Do we need this to create a file with some name, or do we just take a path from the shit and do something there? And if this is the way that why we initially do not use the functions of working with paths: they must return valid values. And when creating your own directory-file, you always need to check the error codes and analyze where the jamb happened (you didn’t have enough rights, you didn’t have enough space, the media was not available or the wrong characters were in the way).

    What do you mean by the term "Valid file name?"

    If you need to check whether such a file exists, you can use the function from Boost'a boost::filesystem::exists()

    • By a valid file name, I mean if there are no <> characters in this string: "/ \ |? * Characters between 0-31, or the string is not a reserved name in DOS - CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, ​​COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, etc. - probeatnah
    • one
      And dear considers that in different operating systems (and even file systems) different restrictions? - gecube
    • one
      If we are talking about a "reserved name in DOS", then the limitations are clear :) Another thing is that within the "language" of this function can not be, because the language and the platform are different entities. - user6550