写入文件内容为何为乱码
#include<map>
#include<string>
#include<iostream>
#include<cstring>
#include<set>
#include<fstream>
#include<algorithm>
#include<iterator>
using namespace std;
set<char*>Word;
//按给定的标点来拆分单词
void SplitString(const string&str)
{
size_t nlen = str.length();
char *strDest = new char[nlen + 1];
strcpy(strDest,str.c_str());
char *flag =",.!:<>?";
char *p = strtok(strDest,flag);
while(p)
{
//cout<<p<<"\n";
Word.insert(p);
//ofs<<p<<"\n";
p = strtok(NULL,flag);
}
delete[]strDest;
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifs("c:\\Music.txt");
if (!ifs.is_open())
{
cout<<"open file failed\n";
return 0;
}
istream_iterator<string>ii(ifs);
istream_iterator<string>eof;
for_each(ii,eof,SplitString);
//将文件中分离的单词写入到文件中
ofstream ofs("c:\\word.txt",ios_base::app);
if (!ofs.is_open())
{
cout<<"open file failed\n";
return 0;
}
ostream_iterator<string>oo(ofs,"\n");
copy(Word.begin(),Word.end(),oo); //为何写入文件中的单词为乱码?
ifs.close();
ofs.close();
system("pause");
return 0;
}
//开发环境 vs2005, XP sp2
//写入文件中的结果 用其它文本文件打开也是乱码
//O:
//:
//?
//饾:
//萚:
//饾:
//O:
//copy 是否像下面这个样子呢?
//template<class T, class U>
//void copy(T first, T last, U out)
//{
// while (first != last)
// {
// *out++=*first++;
// }
//}