I realize it's a colon but not the ascii code that windows rejects in a file name.
Print the value of each character of your filepath variable as an int. The value is simply 58 (0x3A). It being a wide character just makes it the equivalent of a short on Windows (16-bit).
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <fstream>
int main()
{
std::wstring filepath = L"C:\\testfile\x003A";
for (int i = 0; i < (int)filepath.length(); i++)
{
std::wcout << (int)filepath[i] << '\n';
}
}
67
58
92
116
101
115
116
102
105
108
101
58
where is the run time error
I'm not sure what you mean. What kind of run-time error are you expecting? The standard library won't throw an exception here. You can check if the fstream is not in a usable state by doing:
1 2 3 4 5
woutfile.open(filepath);
if (!woutfile)
{
std::cout << "error! cannot open file for writing!\n";
}
(And it's worth mentioning here, wofstream being able to take in a std::wstring is a Microsoft-only extension to the standard library.)
cplusplus forum,
I was able to find a way to create a windows file name with a colon. It's not an ascii
colon but Unicode colon. My goal was to put a time stamp at the end of the file name.
I was able to open the file with Notepad, Wordpad and LibreOffice, do some editing and
write the file back out with no complaints. I also opened it with Firefox, VS 2017 and
Internet Explorer, with no complaints. I don't have MS Office.
There are probably simpler ways to do it but this worked for me.
If you use my code as is you will probably have to create a folder named temp in C:\