Random Unicode problem

I have been trying to make a function that writes multiple files to a folder in the order; file1 file2 file3 ect. After some diagnostic coding I realized it was taking the string file to be equal to a smiley, heart, spade, and club, through the runs of the loop. I think its unicode, but I really don't know. How can I get my file writing function to work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void createFile(int num, string dir, string ext) {
    int i=0;

    while (i < num) {
        ofstream SF;
        string file;
        string write;


        file = i;   // iteration
        write = dir; // path
        write += file; //dir  path iteration
        write += ext; //dir  path name iteration extension
        char fwrite[write.length()]; // new CString
        strcpy(fwrite, write.c_str()); // initialize new CString

        SF.open(fwrite);
        SF << "I am just doing this to waste HD space";
        SF.close();

        i++;
    }
}


It never wrote anything since directories can't have spades or hearts in thier names.
Last edited on
When you use file = i;, it automatically casts i to char. The ASCII code says that number 1 (for example) as a char is a smiley.
To convert it to char '1' you must add an offset to get it to ASCII numbers.

So, you can use file = (i + '0'); to solve it.
Now i is still your number but you add it an offset of the char '0'. So if i is 0 the char cast will return '0', if i is 1 the char cast will return '1' etc.

Hope this helps
Last edited on
It worked!!!
Thank You.
Topic archived. No new replies allowed.