The null/backslash character



Ive got a small problem that i need to fix. I am using ifstream to readline a file full of terminated string lines that contain filenames/paths.

I need to replace all the '\'s in the read lines and turn them into '/'s. Of course attempting to replace "\" wont work in code and i have also tried "\\" but it seems to take that literally.

Any ideas? If it were basic i would be tempted to try chr(asciicode) but am a bit stuck finding the equivalent.

There should be no "\0"s in there so i assume each line string is complete :7
Last edited on
Using the ascii-codes should work. You can find all the codes on this site or on wikipedia:
http://www.cplusplus.com/doc/ascii.html
http://en.wikipedia.org/wiki/Ascii
All you need to do is replace all '\\' with '/'. Using single quotes instructs the compiler to replace that with the appropriate character code (e.g. '@'==64, 'A'==65, 'B'==66, etc.)
Thanks thats exactly what i was after, i have written a simple function to "clean" up the input i am getting from the readline. (\r was causing me a few confusing woes too :) i only worked that one out when i ran a watch on the contents)

in the end i came up with these two handies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //ReadLine
    std::string ReadLine(std::ifstream* file)
    {
        std::string c;
        getline(*file,c);
        c=Replace(c,'\\','/');
        c=Replace(c,'\r','\0');
        c=Replace(c,'\n','\0');

        return c;
    }

    //Replace
    std::string Replace(std::string a, std::string b, std::string c)
    {
        s32 pos ;
        do
        {
            pos = a.find(b);
            if (pos!=-1)  a.replace(pos, b.length(), c);
        }
        while (pos!=-1);
        return a;
    }


LOL what with my Instr and other functions will have a full basic syntax wrapper soon at this rate :D
Last edited on
Topic archived. No new replies allowed.