C++: Is there any good way to read/write without specifically stating character type in function nam
- by Mark L.
I'm having a problem getting a program to read from a file based on a template, for example:
bool parse(basic_ifstream<T> &file)
{
T ch;
locale loc = file.getloc();
basic_string<T> buf;
file.unsetf(ios_base::skipws);
if (file.is_open())
{
while (file >> ch)
{
if(isalnum(ch, loc))
{
buf += ch;
}
else if(!buf.empty())
{
addWord(buf);
buf.clear();
}
}
if(!buf.empty())
{
addWord(buf);
}
return true;
}
return false;
}
This will work when I instantiate this class with <char>, but has problems when I use <wchar_t> (clearly).
Outside of the class, I'm using:
for (iter = mp.begin(); iter != mp.end(); ++iter )
{
cout << iter->first << setw(textwidth - iter->first.length() + 1);
cout << " " << iter->second << endl;
}
To write all of the information from this data struct (it's a map<basic_string<T>, int>), and as predicted, cout explodes if iter->first isn't a char array.
I've looked online and the consensus is to use wcout, but unfortunately, since this program requires that the template can be changed at compile time (<char> - <wchar_t>) I'm not sure how I could get away with simply choosing cout or wcout. That is, unless there way a way to read/write wide characters without changing lots of code.
If this explanation sounds awkwardly complicated, let me know and I'll address it as best I can.