Linux上通过cmake编译uchardet

  1. 下载uchardet源码

GitHub - BYVoid/uchardet: An encoding detector library ported from Mozilla · GitHub

  1. 编译

2.1 mkdir build

2.2 cmake ..

2.3 make

2.4 sudo make install

2.5 使用

cpp 复制代码
#include <iostream>
#include <fstream>
#include <uchardet/uchardet.h>
 
int main() {
    std::ifstream file("example.txt", std::ios::binary);
    if (!file) {
        std::cerr << "无法打开文件" << std::endl;
        return 1;
    }
 
    uchardet_t ud = uchardet_new();
    char buffer[4096];
    while (file.read(buffer, sizeof(buffer))) {
        uchardet_handle_data(ud, buffer, file.gcount());
    }
    uchardet_handle_data(ud, buffer, file.gcount());
    uchardet_data_end(ud);
 
    const char* encoding = uchardet_get_charset(ud);
    std::cout << "检测到的编码: " << encoding << std::endl;
 
    uchardet_delete(ud);
    return 0;
}

g++ -o detect_encoding detect_encoding.cpp -luchardet

./detect_encoding

相关推荐
xiaoxiangsiyan37 分钟前
LNMP + Redis Sentinel 高可用架构部署手册(续)
运维·网络·数据库·redis·缓存·架构·sentinel
AIgorithmGEEK42 分钟前
Linux 权限管理:从「Permission Denied」到「畅行无阻」
linux·权限·chmod·粘滞位·umask·文件权限的构成
BullSmall1 小时前
scanoss-engine 本地最简部署 & 扫描命令(Linux/Anolis OS)
linux·运维·服务器
陈聪.1 小时前
Keepalived 高可用集群:从原理到企业级实践
运维
bellus-1 小时前
Ubuntu 26.04 Miniforge3 安装与配置完整指南
linux·运维·ubuntu
575772 小时前
户外净水器过滤效果怎么看:对照孔径、除菌率和认证三项再下单
运维·服务器
瞬间&永恒~2 小时前
【Docker】(四)Namespace 详解
运维·docker·容器
RD_daoyi2 小时前
Google核心算法不再通知!全年持续滚动更新
大数据·服务器·前端·网络·搜索引擎·.net
爱喝水的木子2 小时前
自建RustDesk私有服务器全攻略
运维·服务器
jieyucx3 小时前
【服务器特性】结合服务器/CMS:解析漏洞实战
运维·服务器·web安全·文件上传