码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 读取、输出txt文件

时间:2019-08-30 18:40:57      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:har   div   库函数   lin   UNC   func   tla   wchar   ram   

下面这段话转自:https://blog.csdn.net/lightlater/article/details/6326338

关于文本文件的文件头

第一 ANSI文件的文件头为空,不需要处理;

第二 UNICODE文件的文件头为0xFF,0xFE共计两个字节,读取时需要偏移两个字节再行读取;

第三 UTF-8文件的文件头为0xEF,0xBB,0xBF共计三个字节,读取时需要偏移三个字节后再行读取;

 

1.ansi格式txt文件

 1 void readAnsiTXT(){
 2     string filename = "ansi.txt";
 3     ifstream fin(filename.c_str());
 4     if (!fin.is_open()){
 5         cout << "open failed!\n";
 6     }
 7     char ch;
 8     string msg = "";
 9     while (fin.get(ch)){
10         msg += ch;
11     }
12     cout << msg << "\n";
13 }

2.Unicode格式

转载:https://blog.csdn.net/hxfhq1314/article/details/80344669

memset函数:https://baike.baidu.com/item/memset/4747579?fr=aladdin

setlocal函数:https://www.runoob.com/cprogramming/c-function-setlocale.html

void readUnicodeTXT(){
    string filename = "unicode.txt";
    ifstream fin;
    fin.open(filename, ios::binary);
    fin.seekg(2, ios::beg);
    wstring wstrLine;
    while (!fin.eof())
    {
        wchar_t wch;
        fin.read((char *)(&wch), 2);
        wstrLine.append(1, wch);
    }
    string str = ws2s(wstrLine);
    str.erase(str.size()-1, 1);//删除结尾重复的一个字符
    cout << str << endl;
}

std::string ws2s(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // C 库函数 char *setlocale(int category, const char *locale) 设置或读取地域化信息。
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

utf8格式:

 1 void readUtf8TXT(){
 2     string str = "utf8.txt";
 3     wstring res=L"";
 4     std::locale loc("chs");
 5     std::wcout.imbue(loc);
 6     std::wifstream wif(str, ios::binary);
 7     codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>* codecvToUnicode = new codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>;
 8     if (wif.is_open()){
 9         wif.imbue(std::locale(wif.getloc(), codecvToUnicode));
10         wstring wline;
11         while (getline(wif, wline)){
12             wstring convert;
13             for (auto c : wline){
14                 if (c != L\0 && c != L?) convert += c;
15             }
16             res = res + convert;        
17         }
18         wif.close();
19     }    
20     for (wstring::iterator i = res.begin(); i != res.end(); i++){//将res中的‘\r‘换成‘\n‘,否则输出异常
21         if (*i == \r){
22             *i = \n;
23         }
24     }
25     wcout << res << endl;
26 }

 

c++ 读取、输出txt文件

标签:har   div   库函数   lin   UNC   func   tla   wchar   ram   

原文地址:https://www.cnblogs.com/Toya/p/11436246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!