Visual Studio C++ 的一个简单示例

Visual Studio 项目属性设置:

项目属性→C/C++→常规→附加包含目录

复制代码
C:\Intel\include\iconv\include;

项目属性→链接器→常规→附加库目录

复制代码
C:\Intel\include\iconv\lib;

项目属性→链接器→输入→附加依赖项

复制代码
iconv.lib;

提示缺少"iconv.dll",需要将iconv.dll拷贝至main.cpp同阶目录。

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include "C:\Intel\include\iconv\include\iconv.h"

//#pragma comment (lib, "iconv.lib")

using namespace std;

string gb2312_to_utf8(const string& input);
bool FileConvertGb2312ToUtf8(string pathFile);
void SaveToLog(string path, string content);


int main(int argc, char* argv[])
{
    int n = 0;

    string path = string(argv[0]);
    string pathLog = path.substr(0, path.find_last_of('\\') + 1) + "log.txt";

    string log = string("123成功了Hello,world!");

    SaveToLog(pathLog, log);

    FileConvertGb2312ToUtf8(pathLog);

    cin >> n;

    return n;
}

bool FileConvertGb2312ToUtf8(string pathFile)
{
    int pos = pathFile.find_last_of("\\");
    string nameFile = pathFile.substr(pos + 1, pathFile.length() - pos - 5);

    ifstream infile(pathFile, ios::binary);
    if (!infile) {
        cerr << "Failed to open input file" << endl;
        return 1;
    }

    string content((istreambuf_iterator<char>(infile)), istreambuf_iterator<char>());
    infile.close();

    string utf8_content = gb2312_to_utf8(content);
    if (utf8_content.empty()) {
        cerr << "Failed to convert encoding" << endl;
        return 1;
    }

    //pathLog = pathLog.replace(pathLog.find_last_of("log") + 1, 0, "_utf8");
    string pathFileNew = pathFile.replace(pathFile.find_last_of(nameFile) + 1, 0, "_utf8");
    ofstream outfile(pathFileNew, ios::binary);
    if (!outfile) {
        cerr << "Failed to open output file" << endl;
        return 1;
    }

    outfile.write(utf8_content.data(), utf8_content.size());
    outfile.close();

    puts("文件转换成功!");
    puts(pathFileNew.c_str());

    return true;
}

string gb2312_to_utf8(const string& input)
{
    iconv_t cd = iconv_open("UTF-8", "GB2312");
    if (cd == (iconv_t)-1) {
        cerr << "iconv_open failed" << endl;
        return "";
    }

    size_t inbytesleft = input.size();
    size_t outbytesleft = inbytesleft * 2;
    char* inbuf = const_cast<char*>(input.c_str());
    char* outbuf = new char[outbytesleft];
    char* outptr = outbuf;

    if (iconv(cd, (const char**)&inbuf, &inbytesleft, &outptr, &outbytesleft) == (size_t)-1) {
        cerr << "iconv failed" << endl;
        iconv_close(cd);
        delete[] outbuf;
        return "";
    }

    string output(outbuf, outptr - outbuf);
    delete[] outbuf;
    iconv_close(cd);
    return output;
}

void SaveToLog(string path, string content)
{
    ofstream outfile(path);

    if (outfile.is_open())
    {
        outfile << content;
        outfile.close();
        puts("文件保存成功");
        puts(path.c_str());
    }
    else
    {
        puts("文件保存失败");
        puts(path.c_str());
    }
}
相关推荐
愚润求学7 分钟前
【Linux】进程间通信(三):命名管道
linux·运维·服务器·开发语言·c++·笔记
一匹电信狗13 分钟前
【数据结构】队列的完整实现
c语言·数据结构·c++·算法·leetcode·排序算法·visual studio
什么半岛铁盒32 分钟前
Linux网络基础全面解析:从协议分层到局域网通信原理
linux·服务器·网络·c++
Da_秀44 分钟前
信奥赛CSP动态规划入门-最小硬币问题
数据结构·c++·笔记·算法·动态规划
tadus_zeng1 小时前
C/C++ 整数类型的长度
c语言·开发语言·c++
我想吃余2 小时前
【C++篇】揭秘STL vector:高效动态数组的深度解析(从使用到模拟实现)
开发语言·c++·笔记·学习·stl
1白天的黑夜14 小时前
动态规划-LCR 089.打家劫舍-力扣(LeetCode)
c++·算法·leetcode·动态规划
阳光_你好6 小时前
简单介绍C++中线性代数运算库Eigen
开发语言·c++·线性代数
ShineSpark7 小时前
C++面试3——const关键字的核心概念、典型场景和易错陷阱
c++·算法·面试
二川bro7 小时前
在Cursor中启用WebStorm/IntelliJ风格快捷键
ide·webstorm