Now suppose you want to rid string of malformed utf-8 characters.
Here is the example that checks utf-8 string and replaces all malformed characters by ‘?’. The same idea as in previous post. Note what it checks only encoding, it is possible that encoding is right, but symbol doesn’t exist.
Also you can use wchar functions, they are more convenient if you want to actively process text with wide characters, but at the same time they are not very flexible if you need to handle encoding errors in input data. Problem is that position in the stream after fgetwc() failure isn’t determined by standard, so following example may work incorrectly on some systems. It tested on ubuntu 8.04 amd64.