I have a little problem, I need to make directory from my c code or c++, doesn't matter.
But directory name must contain characters like ':', ' ','.' in general current time,
when I try to create with mkdir() function I get EINVAL error, but from system("mkdir ...") everything is ok.
How can I solve this problem?
Thank you!!!
Different filesystem formats have different rules about what is and is not a valid character. For ext2 and its descendants, A file name may contain any character except for '/' or '\0'.
For FAT filesystem and its descendants, the list of invalid characters is larger, and includes ':'.
Check to see what filesystem format you are using, and try running your program on a different filesystem.
Related
I have a string and it's going to be a filename . So i want to check if there is a special characters that i'm going to replace them so i won't be a problem when i'm going to create the file . is it a good practice to replace them with "_" ?
i' used this is it correct ? is there other characters excepts alphabet and number can be used on file name ? Which characters should I avoid in file names
String filename = ch.replaceAll(RegExp('[^A-Za-z0-9]'), '_');
The list of allowed filename characters depends on the underlying filesystem. On (most) Unix, anything except / and \0 is allowed. On Windows, the rules get weird. For example, you (usually) can't end a filename with a period; you can't name a file NUL, etc.
Other considerations: It would be confusing to allow spaces at the beginning/end of a filename. Spaces within a filename break certain tools (looking at you, make). Is your filesystem case-sensitive or case-preserving? Does it have a maximum filename length?
Which characters should I avoid in file names?
Wrong question. Do you have a particular need to allow "unusual" characters in filenames?
If these are machine-generated names, just do what you're doing (I prefer hyphens, but that's a stylistic decision). If these are user-generated filenames, just try saving the file -- if it fails, get the user to choose another name.
tl;dr: use URL-safe characters: [A-Za-z0-9_-]+.
I am new to c++. The problem i am facing is with CreateDirectory method.
CreateDirectory("\\ServerName\foldername\",NULL) gives no error but it also doesn't create any directory. However if I write "D:\foldername" instead of "\\ServerName\foldername\" it works perfectly fine.
Any help would be highly appreciable.
"\ServerName\foldername\" is not a valid Windows path
"\\ServerName\foldername\" is valid, but this is the name of the "foldername" share on the "ServerName" network host. This is still not a valid directory you can create.
If ServerName is a valid host name, and if sharename, is a valid share on that host, on which you have write rights, then you could create "\\ServerName\sharename\foldername". But you can't create "\\ServerName\foldername\"
Thanks to Matteo for pointing out that in C strings, the \ must be escaped to \\
You must understand the difference between Fully Qualified vs Relative Paths
For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path. A file name is relative to the current directory if it does not begin with one of the following:
A UNC name of any format, which always start with two backslash
characters ("\")
A disk designator with a backslash, for example "C:\" or "d:\".
A single backslash, for example, "\directory" or "\file.txt". This is
also referred to as an absolute path.
for more Information refer Naming Files, Paths, and Namespaces
I have a char* which only contains ASCII characters (decimal: 32-126). I'm searching for a c++ function which escapes (add a backslash before the character) characters that have special meanings in the unix filesystem like '/' or '.'. I want to open the file with fopen later.
I'm not sure, if manually replacing would be a good option. I don't know all characters with special meanings. I also don't know if '?' or '*' would work with fopen.
Actually Unix (or more specific the SuS) disallows only the byte values '/' and '\0' in file names. Everything else actually is fair game. The exact (in the sense that they're immediately following and followed by a '/') strings "." and ".." are reserved to relative path access, but they are very well valid in a Unix path.
And of course any number and sequence of '.' is perfectly allowed in a Unix filename, as long as another character other than '/' or '\0' is part of the filename. Yes, newline, any control character, they're all perfectly valid Unix filenames.
Of course the file system you're using may have a different idea about what's permissible, but you were just asking about Unix.
Update:
Oh and it should be noted, that Unix doesn't specify dome "parse" method for filenames. Which essentially means, a filename is treated as a binary blob key into a key→value database. It also means, that there's no such thing as "escaping" for Unix filenames.
POSIX filenames don't have a concept of escape characters. There is no way to have a slash as an element of a filename (when the system renders filenames using Unicode you may be able to create a filename which looks as if it contains a slash, though). I think all other printable characters are just fine although using special characters like * and ? in filename will probably cause problems when people try use them from a shell.
I came across this in a source code:
#define DEFAULT_PATHNAME "#SDK_DEFAULT_PATHNAME#"
what does the # symbol denotes in this case ?
Edit:
Camke was used to generate this project.
This value is used as a path to a file
CMake has this wonderful command configure_file which allows your build system to generate a file used in the build where the content (i.e. value) of the variable SDK_DEFAULT_PATHNAME will be put in the location of #SDK_DEFAULT_PATHNAME# in the "configured file".
In this case it's part of the string, nothing special.
On Windows for example, you could have the following string:
#define DEFAULT_PATHNAME "%PATH_TO_SDK%"
with the % character playing the same role. In C++ and in strings in general, it has no meaning (unlike \ which is used to escape characters).
EDIT:
To clarify, esp. with regards to your comment:
that value is used as a path to a file for the program to open, when removing the # the program broke
The operating system may need to read this character, as I mentioned it with the % example on Windows, to consider the path as something to look up in the environment variables for example. Once again, it has no special meaning in C++ or strings in general, but may have for other programs.
A user input string for a destination path can potentially contain spaces or other invalid characters.
Example: " C:\users\username\ \directoryname\ "
Note that this has whitespace on both sides of the path as well as an invalid folder name of just a space in the middle. Checking to see if it is an absolute path is insufficient because that only really handles the leading whitespace. Removing trailing whitespace is also insufficient because you're still left with the invalid space-for-folder-name in the middle.
How do i prove that the path is valid before I attempt to do anything with it?
The only way to "prove" the path is valid is to open it.
SHLWAPI provides a set of path functions which can be used to canonicalize the path or verify that a path seems to be valid. This can be useful to reject obviously bad paths but you still cannot trust that the path is valid without going through the file system.
With NTFS, I believe the path you give is actually valid (though Explorer may not allow you to create a directory with only a space.)
The Boost Filesystem library provides helpers to manipulate files, paths and so... Take a look at the simple ls example and the exists function.
I use GetFileAttributes for checking for existence. Works for both folders (look for the FILE_ATTRIBUTE_DIRECTORY flag in the returned value) and for files. I've done this for years, never had a problem.
If you don't want to open the file you can also use something like the access() function on POSIX-like platforms or _access() and friends on Windows. However, I like the Boost.Filesystem method Ricardo pointed out.