इस लेख में हम C++ STL में std::mbrtowc() फंक्शन के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
std::mbrtowc() क्या है?
std::mbrtowc() फंक्शन C++ STL में एक इनबिल्ट फंक्शन है, जो
सिंटैक्स
size_t mbrtowc( wchar_t* pwc, char* str, size_t n, mbstate_t* ps);
पैरामीटर्स
फंक्शन निम्नलिखित पैरामीटर(पैरामीटर्स) स्वीकार करता है -
- pwc - यह उस लोकेशन का पॉइंटर है जहां हम आउटपुट स्टोर करना चाहते हैं।
- str - कैरेक्टर स्ट्रिंग जिसे इनपुट के रूप में उपयोग किया जाता है।
- n - यह उन बाइट्स की संख्या है जिन्हें चेक किया जाना है।
- ps - जब हम मल्टीबाइट स्ट्रिंग की व्याख्या कर रहे होते हैं तो यह स्टेट ऑब्जेक्ट का पॉइंटर होता है।
रिटर्न वैल्यू
यह फंक्शन निम्नलिखित शर्त के अनुसार अलग-अलग मान लौटाता है -
- 0 - फंक्शन शून्य लौटाएगा जब str में परिवर्तित होने वाला कैरेक्टर NULL हो।
- 1...n - कैरेक्टर स्ट्रिंग *str से परिवर्तित मल्टीबाइट कैरेक्टर के बाइट्स की संख्या।
- -2 - जब अगले n बाइट्स अधूरे हों लेकिन अब तक एक वैध मल्टीबाइट कैरेक्टर हो तो हमें -2 मिलेगा।
- -1 - जब हमें एन्कोडिंग एरर का सामना करना पड़े, तो *pwc में कुछ नहीं लिखा जाता।
उदाहरण
#includeusing namespace std; void print_(const char* ch){ mbstate_t temp = mbstate_t(); int cal = strlen(ch); const char* i = ch + cal; int total; wchar_t con; while ((total = mbrtowc(&con, ch, i - ch, &temp)) > 0){ wcout << "Next " << total << " bytes are the character " << con << '\n'; ch += total; } } int main(){ setlocale(LC_ALL, "en_US.utf8"); const char* len = u8"z\u00df\u6c34"; print_(len); }
आउटपुट
Next 1 bytes are the character z Next 2 bytes are the character ß Next 3 bytes are the character 水
उदाहरण
#includeusing namespace std; void print_(const char* ch){ mbstate_t temp = mbstate_t(); int cal = strlen(ch); const char* i = ch + cal; int total; wchar_t con; while ((total = mbrtowc(&con, ch, i - ch, &temp)) > 0){ wcout << "Next " << total << " bytes are the character " << con << '\n'; ch += total; } } int main(){ setlocale(LC_ALL, "en_US.utf8"); const char* len = u8"\xE2\x88\x83y\xE2\x88\x80x\xC2"; print_(len); }
आउटपुट
Next 3 bytes are the character ∃ Next 1 bytes are the character y Next 3 bytes are the character ∀ Next 1 bytes are the character x