c++ Windows UTF16 L String constant - utf-16

In C++ there is the following statement
wstring tester = L"Работа Центра";
we set the console output
int res = _setmode( _fileno(stdout), _O_U16TEXT);
then we try
wcout << "tester " << tester << endl << flush;
and we get on the console...
tester D D°D±D_Ñ,D° D▌DµD½Ñ,Ñ?D°
So trying to figure out what is happening here instead of getting the proper output.
Is this a code page problem?

Related

CDT inserter and extractor operator problem

this question is a continuing of Error on Constrained Delaunay Triangulation and Gabriel Triangulations
Trying to write a minimal example for that problem I planned insert the triangulation
in a file (std::ofstream) using the << operator of CDT without calling the CGAL::make_conforming_Delaunay_2(cdt); or CGAL::make_conforming_Gabriel_2(cdt),
knowing that until this point everything occured OK.
The triangulation was created without problems and before the exit of the aplication I saved the triangulation in a file using the << operator of CDT. The file was saved without error.
When I tried to read the file using the >> operator of CDT an exception was raised (inside the >> operator). The text of exception is:
CGAL ERROR: assertion violation!\nExpr: s == LEFT_TURN\nFile: C:\\dev\\CGAL-5.3.1\\include\\CGAL\\Triangulation_2.h\nLine: 919
The code I wrote:
int main()
{
CDT cdt;
std::ifstream ArqSuperficie("trian.dtr"); This file was created with << operator of CDT
if (ArqSuperficie.good() == false)
{
return 1;
}
try()
{
ArqSuperficie >> cdt;
}
catch(std::exception& e)
{
std::cerr << "ERROR: exception " << e.what() << std::endl;
}
return 0;
}
Why CDT cannot read a file created by itself, of a triangulation that was created by itself without errors?
Based on the message of the exception I can´t have a clue of what is happening...
Thank you

GDAL VSIS3 and GetRasterBand

I'm trying to access some Landsat data from S3 without making local copies of the files. As a test I wanted to run a simple GetRasterBand on the file but I'm not sure how to go about treating a VSILFILE as a GDALDataset without downloading the file.
GDAL API guide states VSILFILE "cannot be used with any functions other than the "VSI*L" family of functions. They aren't "real" FILE objects."
Snip of my code :
VSILFILE *poVs3Dataset;
//GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
GDALAllRegister();
VSIInstallS3FileHandler();
CPLSetConfigOption( "AWS_ACCESS_KEY_ID", "XXX" );
CPLSetConfigOption( "AWS_SECRET_ACCESS_KEY", "XXX" );
poVs3Dataset = VSIFOpenL(path, "r");
poBand = poVs3Dataset->GetRasterBand( 1 );
Which ultimately and understandably fails
g++ -g -L/usr/local/lib -lgdal stats.cpp
error: ‘VSILFILE’ has no member named ‘GetRasterBand’
Are there any good C++ examples out there I could work through?
Thanks!
Setting my environment variables prior to calling the executable seemed to help:
$> env AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx ./a.out /vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF
This is now my test code to prove to myself I can access with both GDALOpenEx & VSIFOpenExL which works in case it helps someone else:
VSILFILE *fpL;
GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
VSIStatBufL sStat;
const char* const apszAllowedDrivers[] = { "GTiff", NULL };
GDALAllRegister();
poDataset = reinterpret_cast<GDALDataset*>(GDALOpenEx(path, GDAL_OF_READONLY | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL));
if( poDataset == NULL )
{
std::cout << "Couldn't open " << std::endl;
}
poBand = poDataset->GetRasterBand( 1 );
int nXSize = poBand->GetXSize();
int nYSize = poBand->GetYSize();
std::cout << "nXSize : " << nXSize << std::endl;
std::cout << "nYSize : " << nYSize << std::endl;
fpL = VSIFOpenExL(path, "rb", 1);
if( fpL != NULL )
{
....
works!!!

Letters stored as integers

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Please enter an integer between 1 and 5" << endl;
int x; //Selection of menu prompt
cin >> x;
while (x < 1 || x > 5) //Tossing out garbage input
{
cout << "Invalid selection, please make another." << endl;
cin >> x;
}
return 0;
}
When this is run, entering "a" for example, enters the while loop, but does not wait for user input at "cin >> x;" and instead loops infinitely through. Can someone explain to me why this happens and how I might fix the issue? I can only imagine it is something to do with the keyboard buffer.
In this code, it's possible for cin to enter an error state. If the user does not enter an integer, it will fail.
That is, if the user enters a, then cin >> x does not set x, and future calls to cin >> x do not block. You see an endless loop.
You can check for this failure status and clear it. before continuing using code similar to:
if (cin.fail())
{
cin.clear();
cin.ignore();
cerr << "Invalid selection, please make another." << endl;
}
You really should use cin.clear() and cin.ignore() after accepting the input.
cin.clear() clears the error flag on cin, and then cin.ignore(5000, '\n') skips to the next newline. It will skip up to 5000 characters, so the code is assuming the user will not put in a very long.

Filestreams reading and writing to text file, then convert to binary in c++

What I am trying to do is fairly elementary however I am having trouble with my project. My project is too large to include everything, so I will just include the two functions that I am writing along with what the txt file looks like. This is in c++.
bookmark.cfg
No Title
0 0 0 0 0 0
No Title
1 1 1 1 1 1
No Title
2 2 2 2 2 2
No Title
3 3 3 3 3 3
No Title
4 4 4 4 4 4
No Title
5 5 5 5 5 5
These are my two functions for writing and reading to the text file and my class's private structure
struct BookMark {
std::string strFilename;
unsigned id;
unsigned bookID;
unsigned chapterNumber;
unsigned pageNumber;
unsigned lineNumber;
unsigned columnNumber;
}; // BookMark
// ----------------------------------------------------------------------------
// readConfigFile()
bool BookManager::readConfigFile() {
using namespace std;
// Just To Be Safe Incase This Function Is Called In Multiple Places
_mBookMarks.clear();
ifstream inFile( _strConfigFilename );
if ( inFile.fail() ) {
throw ExceptionHandler( __FUNCTION__ + std::string( " failed, could not open " ) + _strConfigFilename + std::string( " \nfor reading in book mark information \nInvalid file or file does not exist" ) );
}
// Read In The Book Mark Contents
std::vector<BookMark> vBookMarks;
BookMark bookMark;
string tempString = "";
if ( inFile.is_open() ) {
while ( !inFile.eof() ) {
BookMark bookMark;
getline( inFile, bookMark.strFilename );
inFile >> bookMark.id;
inFile >> bookMark.bookID;
inFile >> bookMark.chapterNumber;
inFile >> bookMark.pageNumber;
inFile >> bookMark.lineNumber;
inFile >> bookMark.columnNumber;
getline( iniFile, tempString );
//_mBookMarks.insert( make_pair( bookMark.id, bookMark ) );
vBookMarks.push_back( bookMark );
}
}
inFile.close();
return true;
} // readConfigFile
// ----------------------------------------------------------------------------
// writeConfigFile()
bool BookManager::writeConfigFile() {
using namespace std;
ofstream outFile;
outFile.open( _strConfigFilename, fstream::out );
if ( outFile.fail() ) {
throw ExceptionHandler( __FUNCTION__ + std::string( " failed, could not open " ) + _strConfigFilename + std::string( " \nfor writing book mark contents to file." ) );
}
// Write Out Book Mark Contents
if ( outFile.is_open() ) {
unsigned i = 0;
for ( i = 0; i < _mBookMarks.size(); i++ ) {
outFile << _mBookMarks.at( i ).strFilename << endl;
outFile << _mBookMarks.at( i ).id << " ";
outFile << _mBookMarks.at( i ).bookID << " ";
outFile << _mBookMarks.at( i ).chapterNumber << " ";
outFile << _mBookMarks.at( i ).pageNumber << " ";
outFile << _mBookMarks.at( i ).lineNumber << " ";
outFile << _mBookMarks.at( i ).columnNumber << endl << endl;
}
}
outFile.close();
return true;
} // writeConfigFile
The problem I am having is, when I call the write function all the text is being displayed properly in my text file. The first line should be a string that contains the book's title or filename. The second line should be all unsigned ints to specify parameters to know the position of the bookmark's location. As of now I just populated my classes structure with arbitrary data using a for loop and incrementing each bookmarks parameters just to test these functions. Somewhere else in my code I call my write method first to create this text file and write the contents. This seems to be working fine. Then I call my read method to read in the file and populate a temporary vector of my structure to see if the contents being read from the file are valid. Once I get this to work properly I'll then just populate my class's member variable structure instead.
Here I am reading in the first line using getline function then I am using the stream operators to get the rest of the contents. While I am debugging my code going through the read method and checking my temp vector the first element has the correct value with No Title and each of the parameters are 0. When I check the next element there is no string in the next BookMark structure object and all values are 0. Also there should only be 5 elements in my temp vector and it should break out of the loop, but it continues and never breaks out of the loop. Why is the code behaving like this? What is it that I am doing wrong? How can I change this to get the behavior I am looking for?
If you need to see this class in full let me know, but I think the rest of the class is not important as to what I am trying to do, only these two functions should be enough to describe my situation. Once I get this to work properly then I'll just change the read and write methods to work in binary as opposed to text.
I believe I have solved my own problem. In my read method, I had to add a second getline( this fileStream, tempString ); And now my code appears to be working properly. My vector has the correct values and it only has 5 elements and now breaks out of the loop.

wcout function does not print a french character

I am using the wcin in order to store a single character in a wchar_t. Then I try to print it with a wcout call and the french character 'é' : but I can't see it at my console.
My compiler is g++ 4.5.4 and my OS is Ubuntu 12.10 64 bits.
Here is my attempt (wideChars.cpp) :
#include <iostream>
int main(){
using namespace std;
wchar_t aChar;
cout << "Enter your char : ";
wcin >> aChar;
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
When I lauch the programm :
$ ./wideChars
Enter your char : é
You entered .
So, what's wrong with this code ?
First, add some error checking. Test what does wcin.good() return after the input and what does wcout.good() return after the "You entered" print? I suspect one of those will return false.
What are your LANG and LC_* environment variables set to?
Then try to fix this by adding this at the top of your main(): wcin.imbue(std::locale("")); wcout.imbue(std::locale(""));
I do not have my Ubuntu at hand right now, so I am flying blind here and mostly guessing.
UPDATE
If the above suggestion does not help then try to construct locale like this and imbue() this locale instead.
std::locale loc (
std::locale (),
new std::codecvt_byname<wchar_t, char, std::mbstate_t>("")));
UPDATE 2
Here is what works for me. The key is to set the C locale as well. IMHO, this is a bug in GNU C++ standard library implementation. Unless I am mistaken, setting std::locale::global(""); should also set the C library locale.
#include <iostream>
#include <locale>
#include <clocale>
#define DUMP(x) do { std::wcerr << #x ": " << x << "\n"; } while (0)
int main(){
using namespace std;
std::locale loc ("");
std::locale::global (loc);
DUMP(std::setlocale(LC_ALL, NULL));
DUMP(std::setlocale(LC_ALL, ""));
wcin.imbue (loc);
DUMP (wcin.good());
wchar_t aChar = 0;
wcin >> aChar;
DUMP (wcin.good());
DUMP ((int)aChar);
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
UPDATE 3
I am confused, now I cannot reproduce it again and setting std::locale::global(loc); seems to do the right thing wrt/ the C locale as well.