Getting the system locale in C++

My recent project was a console application which need to work in both Japanese and in English.
here I had to write a text to console, and this text differs based on the locale().In this situation I need to find the current system locale and do actions correspondingly.
Here I will explain how we can read system locale in c++

string locale = setlocale(LC_ALL, "");//this function sets applications locale to current system locale
cout<<"default locale:"<<locale;
if (sLocale == "Japanese_Japan.932")
{
    //Do actions
}else 
{
    //Do actions
}

Setting maximum number of files that can be simultaneously opened in c++

In one of my recent project I had to open ‘N’ number of file handles. When number of handles reaches a particular limit(512) open file function failed subsequently.I was wondered this behavior,
but after referring further I could understand that in C++ at stdio level there is a default 512 limit on the number of simultaneously opened file descriptors. In order to change the default count _setmaxstdio() function can be used.
Following is the syntax for it.

int _setmaxstdio(
   int newmax 
);

Also note that at even using this function at stdio level maximum number of file descriptors opened simultaneously can be up to 2048.

Reading and writing a Unicode file in C++

In one of my recent project I had to read and write a CSV file having JAPANESE strings.Here I will explain how we can read and write a Unicode file in c++.


std::wstring readUnicodeFile(const char* filename)
{
    std::ifstream wif(filename);
    std::stringstream wss;
    wss << wif.rdbuf();
    std::string  const &str = wss.str();
    std::wstring wstr;
    wstr.resize(str.size()/sizeof(wchar_t));
    std::memcpy(&wstr[0],str.c_str(),str.size()); // copy data into wstring
    return wstr;
}

void WriteUnicodetoFile(const char* myFile,  wstring& ws){
    std::ofstream outFile(myFile, std::ios::out | std::ios::binary);
    outFile.write((char *) ws.c_str(), ws.length() * sizeof(wchar_t));
    outFile.close();

}

int main(int argc, _TCHAR* argv[])
{
    wstring sText = readUnicodeFile("inputUnicode.txt");
    WriteUnicodetoFile("ouputUnicode.txt",  sText);
}

Counting the number of occurrences of a particular character in a file with a single line of code in C++

Here I will explain how to count the number of occurrences of a particular character say newline character ‘\n’ in a file using STL std::count() algorithm.
following is the code
suppose I have a Csv file “test.csv”, I need to count the number of rows in it(number of ‘\n’ character in the file)


/*
* Open the csv file
*/
ifstream InCsvFile;
InCsvFile.open("test.csv", ios::in ); 

__int64 nLineCount = std::count(std::istreambuf_iterator<char>(InCsvFile), 
        std::istreambuf_iterator<char>(), '\n');