UTF8, with free compiler

Mar 11, 2016 at 11:47pm
Hello, I have this code that runs perfectly on my Visual Studio 2010, but I now need it to compile on some free compiler, for a friend who doesn't have VS. I tried on regular GCC within Code::Blocks, but it won't pass, it gives a lot of strange errors.
Also tried with Borland C++, not working. Maybe its just a thing of changing it a bit?

Code is about reading from file in UTF8 encoding.

Again, code works fine on VS2010.

Anybody have any idea? Thanks.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <Windows.h>
#include <locale>
#include <codecvt>
#include <string>
#include <cstdlib>


///convert as ANSI - display as Unicode
std::wstring test1(const char* filenamein)
{
	const std::locale empty_locale = std::locale::empty();
    typedef std::codecvt_utf8<wchar_t> converter_type;
    const converter_type* converter = new converter_type;
    const std::locale utf8_locale = std::locale(empty_locale, converter);

	std::wifstream fs(filenamein);
	if(!fs.good()) 
	{ 
		std::cout << "cannot open input file [" << filenamein << "]\n" << std::endl;  
		
		return NULL; 
	}

	fs.imbue(utf8_locale);

	wchar_t c; 
	std::wstring s;

	while(fs.get(c)) 
	{ 
		s.push_back(c); 
		std::cout << '.' << std::flush; 
	}

	return s;
	
}

int printToFile(const char* filenameout, std::wstring line)
{
	std::wofstream fs;

	fs.open(filenameout);

	if(!fs.is_open())
		return -1;

	const std::locale empty_locale = std::locale::empty();
    typedef std::codecvt_utf8<wchar_t> converter_type;
    const converter_type* converter = new converter_type;
    const std::locale utf8_locale = std::locale(empty_locale, converter);

	fs.imbue(utf8_locale);

	std::wstring begin = L"&#";
	std::wstring end = L";";


	for(unsigned i = 1; i < line.length(); i++)
	{
		if(line[i] <= 126)  //if its standard letter just print to file
			fs << (char)line[i];
		else  //otherwise do this.
		{
			std::wstring write = L"";

			std::wostringstream ss;
			ss << (int)line[i];

			fs << begin;

			write = ss.str();
			fs << write;

			fs << end;

		}
	}

	fs << std::endl;

	fs.close();

	return 1;
}

int main(int argc, char* argv[])
{
	std::wstring line = test1(argv[1]);

	if(printToFile(argv[2], line) == 1)
		std::cout << "Writing success!" << std::endl;
	else std::cout << "Writing failed!" << std::endl;



	return 0;
}
Mar 11, 2016 at 11:57pm
http://www.cplusplus.com/reference/codecvt/
says that codecvt is a C++11 feature.

The default mode of GCC is not C++11. You have to use option -std=c++11 (or similar, check the manual of the version of the GCC that you use).


When there are a lot of errors, the best bet is to look at the first of them. The rest can be just "collateral damage". Perhaps even show the error here (were are not able to analyze "strange" without details.)
Mar 12, 2016 at 12:11am
Hi. Thanks, this is a good comment. I tried enabling C++11 in Code::Blocks (settings->compiler-> have g++ follow c++11 ISO C++ language standard

But it still says
1
2
fatal error: codecvt: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


I remember having troubles earlier with C++11 on code::blocks, but somehow managed to work around it so it didn't bother me much then.
Mar 12, 2016 at 12:22am
Which version of GCC do you have? (gcc --version)

Quite new is required:
http://stackoverflow.com/questions/24497956/is-codecvt-not-supported-by-clang-or-gcc
Mar 12, 2016 at 12:26am
I have 4.7.1 version of GCC.

When I tried adding '-stdlib=libc++' to compiler flags, i got this message from C::B

error: unrecognized command line option '-stdlib=libc++'
Mar 12, 2016 at 12:29am
That option (and library) is for Clang, a different (free) compiler.
GCC 5 exists already.
C::B probably allows change of compiler.
Mar 12, 2016 at 12:36am
Hi. Yes, C::B can change compiler.

Can you tell me if you can compile the code? If you aready have GCC 5.3, or something newer than mine.

I don't have the problem with updating my compiler, I just want to see if it will solve my problem.
Mar 12, 2016 at 12:43am
Why not just compile an executable with VS (in release mode) and give your friend the exe and required redistributables?
Mar 12, 2016 at 12:46am
He wants to continue developing the application and use the project I did as only the part of the entire code.
Also, he might want to make some slight changes in printouts etc...
Mar 12, 2016 at 1:19am
Ah, valid point.

AFAIK, GCC doesn't have the empty() locale constructor -- it's a MS thing... Just get rid of the assignment and it should work fine:

13
14
15
std::wstring test1(const char* filenamein)
{
	const std::locale empty_locale;  // = std::locale::empty(); 
49
50
51
52
	if(!fs.is_open())
		return -1;

	const std::locale empty_locale;  // = std::locale::empty(); 

Hope this helps.
Mar 12, 2016 at 1:38am
Thanks for your help. When I remove empty() from the code, a lot of other errors pop up. Related to this line, most of them

 
typedef std::codecvt_utf8<wchar_t> converter_type;


I discovered that MS offers some 'Community' distribution of VS, so, if its anything like Qt community, it should be plenty.
Gonna give that a try.

Thanks, guys!
Topic archived. No new replies allowed.