# Illegal Characters

This is a discussion on Illegal Characters within the Windows Programming forums, part of the Platform Specific Boards category; Okay, some uri's look like this: http://www.example:80/files/text.txt My program is designed to, among other things, take the above uri and ...

1. ## Illegal Characters

Okay, some uri's look like this:
http://www.example:80/files/text.txt

My program is designed to, among other things, take the above uri and create a local directory from it
e.g. C:\www.example:80\files\text.txt

Of course, the ":" in the uri is an illegal character and CreateFile would fail when attempting to create the folder \www.example:80\

So, I created a function that removes illegal characters like ":" (I included the code at the end of this message)

Unfortunately, even when I replace the illegal characters CreateFile still fails as if the ":" has not been replaced

Yes, I confirmed that the ":" is the only problem by removing it from the uri -- without the ":" it works perfectly

Why is CreateFile failing under these circumstances??????

Code:

int replaceillegalchars(char *string, char replace, char *output)
{
int len, outindex = 0;
BOOL bFound = FALSE;
len = lstrlen(string);

for(int z = 0; z < len; z++)
{
if(string[z] == '<' || string[z] == '>' || string[z] == ':' || string[z] == '*'
|| string[z] == '?' || string[z] == '"' || string[z] == '|')
{
output[outindex] = replace;
bFound = TRUE;
}
else
output[outindex] = string[z];

outindex++;
}
output[outindex] = '\0';

if(!bFound)
return 0;

return 1;
}

thanks

2. Looks like your function is fine, just one note, you should use '\"' the escape char for a " is \" Also I simplified the funct a bit. But, the prob with CreateFile() must be somewhere else. Are you sure you're using the modified string and not the original one when calling CreateFile? Also, if you want to create a local mirror of the directory structure, you'll need to replace the forward slashes with backslashes but in your prog use double backslashes which are actually the C++ escape char for a backslash:

http://www.example:80/files/text.txt
C:\\www.example:80\\files\\text.txt

Code:
#include<iostream.h>
int replaceillegalchars(char *string, char replace, char *output)
{
int len;
bool bFound = false;
len = strlen(string);

strcpy(output, string);

for(int z = 0; z < len; z++)
{
if(string[z] == '<' || string[z] == '>' || string[z] == ':' || string[z] == '*'
|| string[z] == '?' || string[z] == '\"' || string[z] == '|')
{
output[z] = replace;
bFound = true;
}
}
if(!bFound)return 0;
return 1;
}

void main()
{
char str[] = "http//:www.sub\"duck.com";
char buff[256];
replaceillegalchars(str, '_', buff);
cout<<buff;
cin.get();
}
Hope that helps
-Futura

3. >>>Are you sure you're using the modified string and not the original one when calling CreateFile? >>>

Yes, that was the first thing I checked, thanks.

The problem was actually with the function I created that creates the necessary directories long before CreateFile is called -- an oversight on my part...sorry about that.

thanks again....

4. Well,
Glad you got it figured out