Filenames
HI (again )!
How can i get to know, if a string contains "Sonderzeichen" (sorry, i don''t know the English word - I mean characters, that are not allowed in filenames, such as < > / \ ?....)
It would be cool, if you could help me
- - - - - - - - - - - - - -
Andreas Mähler
Wolf359 Interactive
bool error = false;char filename[30];// do some stuff to the filename like have a user enter itfor (int i = 0; i < strlen(filename); i++){ if (filename<i> == '<' || filename[i] == '>' || filename[i] == '/' || filename[i] == '?' || // and so forth) { error = true; }}if (error){ // error in filename}else{ // no error in filename}
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
Edited by - ncsu121978 on July 10, 2000 4:29:13 PM
Edited by - ncsu121978 on July 10, 2000 4:30:01 PM
I don''t think we really have a name for them, I mostly see them called "invalid file characters".
I have 2 methods...
If it is possible for the entire set of non-printable characters to be introduced as well as invalid file characters, I check to make sure that each letter in the file name is useable according to ascii value bounds. This isn''t too bad as usually file names aren''t that long.
If you are assured that the filename contains only printable characters, you can do a search to see if any of the characters that are considered invalid exist in the string. There aren''t that many printable characters that you can''t use in a filename, really.
-fel
I have 2 methods...
If it is possible for the entire set of non-printable characters to be introduced as well as invalid file characters, I check to make sure that each letter in the file name is useable according to ascii value bounds. This isn''t too bad as usually file names aren''t that long.
If you are assured that the filename contains only printable characters, you can do a search to see if any of the characters that are considered invalid exist in the string. There aren''t that many printable characters that you can''t use in a filename, really.
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Hmm, you beat me.
That works tho personally I prefer to use strchr and have it search for me.
char *strchr(const char *s, int c)
will return a pointer to the first occurence of the character c in string s, or will return NULL if character c doesn''t exist in string s (which would be the case you would want).
-fel
That works tho personally I prefer to use strchr and have it search for me.
char *strchr(const char *s, int c)
will return a pointer to the first occurence of the character c in string s, or will return NULL if character c doesn''t exist in string s (which would be the case you would want).
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
The C standard provides a function to do this for you: strpbrk. It returns a pointer to the first occurrence of any character from a set of characters in a string. For example:
if (strpbrk(filename, "\/:?@")) fprintf(stderr, "Invalid characters in filename.\n");
July 13, 2000 05:43 PM
Please keep in mind that the set of characters that are invalid in a filename is different from OS to OS and even from filesystem to filesystem in the same OS. An example would be that spaces are not allowed in filenames in DOS but are in NT. I believe that there are slightly different rules for FAT16, FAT32, NTFS, etc and the user could have all of them mounted at once.
The best thing to do is take whatever the user types and try to use it. Then only complain to the user if the OS tells you that you that the filename is bad.
The best thing to do is take whatever the user types and try to use it. Then only complain to the user if the OS tells you that you that the filename is bad.
I am writing a game for Win95/98/2000/ML, so i think this is not the problem. What you suggested is OK, but not so important for me, because i want my program to create a playerfile(name) from the callsign (in which all chars are allowed). All forbidden chars are replaced by an "x" in the filename. The original filename is stored in the file.
That''s it
Andy
- - - - - - - - - - - - - -
Andreas Mähler
Wolf359 Interactive
void CreatePlayerFilename (char* InputString, char* OutputString){ BOOL Done = FALSE; char* InvalidChar = NULL; char* Filename = InputString; char* Filename2 = ""; char* ReplaceChar = "x"; WIN32_FIND_DATA dFindData; BYTE n = 0; while (!Done) { Done = TRUE; InvalidChar = strpbrk (Filename, "/\?*""<>:|."); if (InvalidChar != NULL) { Done = FALSE; InvalidChar[0] = ReplaceChar[0]; continue; } wsprintf (Filename2, "Players\\%s.mwp", Filename); if (FindFirstFile (Filename2, &dFindData) != INVALID_HANDLE_VALUE) { Done = FALSE; n++; wsprintf (Filename, "%s%d", Filename, n); } } OutputString = Filename2;}
That''s it
Andy
- - - - - - - - - - - - - -
Andreas Mähler
Wolf359 Interactive
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement