cpp: read and write utf-8 text file using vs 2022

 叮𪠽𪠽𪀋𪀋𪀋𪠽

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*****************************************************************//**
 * \file   geovindu.h
 * \brief  业务操作方法
 *
 * \author geovindu,Geovin Du
 * \date   2023-04-22
***********************************************************************/
/**
 * https://learn.microsoft.com/zh-cn/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170
 *
 * .
 */
 
 
#pragma once
 
#define _UNICODE
 
#ifndef GEOVINDU_H
#define GEOVINDU_H
 
#include <iostream>
#include <windows.h>
#include<string>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include <iostream>
#include <windows.h>
 
 
namespace geovindu
{
 
    class Geovin
    {
 
    private:
 
    public:
 
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        //string to_utf8(const wchar_t* buffer, int len);
        /// <summary>
        ///
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        //string to_utf8(const wstring& str);
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="str"></param>
        //void createFile(wstring& str);
        /// <summary>
        /// 写成UTF-8文本文件
        /// </summary>
        void createFile();
 
    };
 
};
 
 
#endif
#define UNICODE

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define _UNICODE
 
#include <iostream>
#include <windows.h>
#include<string>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include "geovindu.h"
 
 
using namespace std;
 
namespace geovindu
{
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="len"></param>
    /// <returns></returns>
    string to_utf8(const wchar_t* buffer, int len)
    {
        int nChars = ::WideCharToMultiByte(
            CP_UTF8,
            0,
            buffer,
            len,
            NULL,
            0,
            NULL,
            NULL);
        if (nChars == 0) return "";
        string newbuffer;
        newbuffer.resize(nChars);
        ::WideCharToMultiByte(
            CP_UTF8,
            0,
            buffer,
            len,
            const_cast<char*>(newbuffer.c_str()),
            nChars,
            NULL,
            NULL);
 
        return newbuffer;
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    string to_utf8(const wstring& str)
    {
        return to_utf8(str.c_str(), (int)str.size());
    }
    /// <summary>
    ///
    /// </summary>
    void createFile(wstring& strchinese)
    {
 
        ofstream testFile;
 
        testFile.open("demoinput.txt", std::ios::out | std::ios::binary);
 
        //std::wstring text = strchinese;          
 
        std::string outtext = to_utf8(strchinese);
 
        testFile << outtext;
 
        testFile.close();
 
    }
    ///<summary>
    /// 现有的文本写成UTF-8文本文件
    ///</summary>
    void Geovin::createFile()
    {
 
        ofstream testFile;
 
        testFile.open("geovindudemo.txt", std::ios::out | std::ios::binary);
 
        std::wstring text =
            L"涂聚文,你好,世界欢迎你!동생은 점수를 많이 땄어요\t geovindu\n Geovin Du \nНематериальное наследие водной рифмы\n"
            L"奇松・怪石・雲海と温泉\t大黄河を望む炳霊寺、驚異の張掖丹霞とシルクロードの要所9日間\n"
            L"Tours más solicitados\tParaíso en la Tierra - 13 Días\n"
            L"Entdecken Sie die schönsten Reiseziele von China mit unseren empfohlenen Touren.\n"
            L"Explorez les destinations les plus étonnantes de la Chine avec les visites recommandées.\n"
            L"\n";
 
        std::string outtext = to_utf8(text);
 
        testFile << outtext;
 
        testFile.close();
 
    }
 
};
 
#define UNICODE

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// ConsoleTextFileDemoApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//geovindu Geovin Du
#define _UNICODE
#define _CRT_SECURE_NO_WARNINGS
 
 
#include <iostream>
#include <windows.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <codecvt>
#include <assert.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <io.h>
#include <vector>
 
#include "ConvertEncode.h"
#include "geovindu.h"
#include "FileHelper.h"
 
 
using namespace std;
using namespace geovindu;
 
 
 
 
 
/// <summary>
/// 写成UTF-8文本文件
/// </summary>
void createFile(wstring& strchinese)
{
    ConvertEncode encode;
    wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
 
    ofstream testFile;
 
    testFile.open("geovinduinput.txt", std::ios::out | std::ios::binary);
 
    //std::wstring text = strchinese;
 
 
    std::string outtext = convert.to_bytes(strchinese);//
 
    testFile << outtext;
 
    testFile.close();
 
    std::string narrowStr = convert.to_bytes(strchinese);
    {
        std::ofstream ofs("geovinduinput2.txt");            //文件是utf8编码
        ofs << narrowStr;
    }
 
}
/// <summary>
/// 读文写文件 utf-8的文本文件
/// </summary>
void readfile()
{
    ConvertEncode encode;
    char sname[50];
    string stuID;//学号
    int num;//编号
    double english;//英语成绩
    double math;//数学成绩
    double cpp;//C++成绩
    vector<string> lines;
    string line;
    ifstream fin;
    fin.open("geovinduinput.txt", ios::in); //utf-8文件读
    if (!fin)
    {
        cout << "Fail to open the file!" << endl;
        exit(0);
    }
 
    //创建链表,并保存数据
    while (1)
    {
        if (!(fin >> sname >> stuID >> english >> math >> cpp))//从文件中读取数据 中文没有读出来
        {
            break;
        }
        else
        {
            cout << encode.UTF8ToGBDu(sname) << "\t" << stuID << "\t" << english << "\t" << math << "\t" << cpp << endl;
        }
    }
 
    while (getline(fin, line)) {
        lines.push_back(line);
    }
    fin.close();
    //cout << encode.UTF8ToGBDu(sname) << "\t" << stuID << "\t" << english << "\t" << math << "\t" << cpp << endl;
     
}
 
 
const int FBLOCK_MAX_BYTES = 256;
/*
// File Type.
typedef enum FileType
{
    ANSI = 0,
    unicode,
    UTF8,
}FILETYPE;
 
FILETYPE GetTextFileType(const std::string& strFileName);
 
int UnicodeToANSI(char* pDes, const wchar_t* pSrc);
*/
 
int main(void)
{
    std::cout << "Hello World! 涂聚文\n";
 
     
 
 
 
 
    /*代码无用
            FileHelper helper;
            // file test.
            std::string strFileANSI = "studentANSI.txt";
            std::string strFileUNICODE = "student.txt";
            std::string strFileUTF8 = "geovindudemo.txt";
 
            // please change the file name to test.
            std::string strFileName = strFileUTF8;
            //文件类型没有读对
            TEXTFILETYPE fileType = helper.GetTextFileType(strFileName);
 
            if (TextFileType_UNICODE == fileType)
            {
                wchar_t szBuf[FBLOCK_MAX_BYTES];
                memset(szBuf, 0, sizeof(wchar_t) * FBLOCK_MAX_BYTES);
 
                std::string strMessage;
 
                FILE* fp = NULL;
                fp = fopen(strFileName.c_str(), "rb");
                if (fp != NULL)
                {
                    // Unicode file should offset wchar_t bits(2 byte) from start.
                    fseek(fp, sizeof(wchar_t), 0);
                    while (fread(szBuf, sizeof(wchar_t), FBLOCK_MAX_BYTES, fp) > 0)
                    {
                        char szTemp[FBLOCK_MAX_BYTES] = { 0 };
 
                        helper.UnicodeToANSI(szTemp, szBuf);
                        strMessage += szTemp;
                        memset(szBuf, 0, sizeof(wchar_t) * FBLOCK_MAX_BYTES);
                    }
                }
                cout << "UNICODE" << endl;
                std::cout << strMessage << std::endl;
 
                fclose(fp);
            }
            else if (TextFileType_UTF8 == fileType)
            {
                char szBuf[FBLOCK_MAX_BYTES];
                memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
 
                std::string strMessage;
 
                FILE* fp = NULL;
                fp = fopen(strFileName.c_str(), "rb");
                if (fp != NULL)
                {
                    // UTF-8 file should offset 3 byte from start position.
                    fseek(fp, sizeof(char) * 3, 0);
                    while (fread(szBuf, sizeof(char), FBLOCK_MAX_BYTES, fp) > 0)
                    {
                        strMessage += szBuf;
                        memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
                    }
                }
                cout << "utf-8" << endl;
                std::cout << strMessage << std::endl;
 
                fclose(fp);
            }
            else
            {
                char szBuf[FBLOCK_MAX_BYTES];
                memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
 
                std::string strMessage;
 
                FILE* fp = NULL;
                fp = fopen(strFileName.c_str(), "rb");
                if (fp != NULL)
                {
                    // common file do not offset.
                    while (fread(szBuf, sizeof(char), FBLOCK_MAX_BYTES, fp) > 0)
                    {
                        strMessage += szBuf;
                        memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
                    }
                }
                cout << "ANSI" << endl;
                std::cout << strMessage << std::endl;
 
                fclose(fp);
 
 
            }
 
 
    */
 
 
 
 
    readfile();
    //读内容
    //std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
    //std::ifstream ifs(L"geovinduinput.txt");
    //while (!ifs.eof())
    //{
    //  string line;
    //  getline(ifs, line);
    //  wstring wb = conv.from_bytes(line);
    //  wcout.imbue(locale("chs"));         //更改区域设置 只为控制台输出显示 其他语言显示不了,中文可以
    //  wcout << wb << endl;
    //}
    //ifs.close();
 
 
    Geovin geovin;
    geovin.createFile();
    wstring allstr;
    wstring sname;
    wstring stuID;//学号
    int num;//编号
    double english;//英语成绩
    double math;//数学成绩
    double cpp;//C++成绩
    int location = 0;//位置编号
    int flag = 0;//标记是否有对应的编号
 
    wcout << "请输入新增学生的信息" << endl;
    wcout << "姓名\t" << "学号\t" << "英语\t" << "数学\t" << "C++\t" << endl;
    wcin.imbue(locale("chs"));//获取的是中文
    
    wcin >> sname >> stuID >> english >> math >> cpp;
 
    //allstr = sname + ' ' + stuID;
    allstr.append(sname); //C++ wstring::append
    allstr.append(L"\t");
    allstr.append(stuID);
    allstr.append(L"\t");
    allstr.append(to_wstring(english));
    allstr.append(L"\t");
    allstr.append(to_wstring(math));
    allstr.append(L"\t");
    allstr.append(to_wstring(cpp));
   // createFile(allstr);
 
 
    system("pause");
    return 0;
 
 
}
 
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
 
// 入门使用技巧:
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
 
 
 
 
 
#define UNICODE

  

 

 

 

 

 

 

 

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示