problems with strange characters

Hi everyone!

I am having problems to create a file in a directory that contains strange characters. I am using Visual Studio 2015 in Windows 10.

This is the whole path I am trying to create :

"C:\\āōēāōēāōē\\file.icc"

And this is the code I am using to open the file :

1
2
3
4
5
6
7
8
9
10
11
12
13

void openFile(std::wstring pathToCopy)
{
	std::wofstream file(pathToCopy, std::wofstream::out | std::wofstream::binary);

	if (!file.is_open())
	{
		return false;
	}

	file.close();
}


The code is not working because the call 'file.is_open()' returns false. The thing is that this call is actually working with Chinese or Japanese characters, but these characters 'āōēāōēāōē' are causing a lot of trouble.

Does anybody know why?

Thanks a lot!
The first problem I see is that you're trying to return from a void function. That shouldn't even compile.

Also, file.close() is not needed. It is handled automatically in the destructor using RAII principles.

Does this code not work for you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Test239658.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h" // uncomment if it Visual Studio complains

#include <iostream>
#include <fstream>

bool openFile(std::wstring pathToCopy)
{
	std::wofstream file(pathToCopy, std::wofstream::out | std::wofstream::binary);

	return file.is_open();
}

int main()
{
	if (openFile(L"C:/āōēāōēāōē/file.icc"))
	{
		std::cout << "success" << std::endl;
	}
	else
	{
		std::cout << "failure" << std::endl;
	}
}


PS: Visual Studio encodes it in 16-bit windows-unicode, I think.
PPS: \\ (backslash) is almost never needed when opening files in C++, Windows will handle forward slashes just fine (/).
Last edited on
Hi Ganado! thank you very much for your response. The problem was not my code, the string that I was receiving was not properly encoded in UTF8.

Thank you very much anyway!
Topic archived. No new replies allowed.