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. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    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. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    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. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    >>>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. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Well,
    Glad you got it figured out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Detecting illegal characters
    By paul_harris77 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 02:58 PM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21