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

相关推荐
a5986524 小时前
云手机怎么选?ARM服务器原生架构和X86模拟器差别很大
服务器·arm开发·智能手机
程序员清风11 小时前
数据处理脚本工程化:日志、配置、命令行与测试
服务器·开发语言·数据库
readhere12 小时前
从ZYNQ7000开发板,学会启动嵌入式Linux系统——入门学习:EBAZ4205篇目录
linux·启动·嵌入式linux·zynq7000·ebaz4205
打工仔折腾 AI13 小时前
在 Ubuntu ARM64 部署 Q1 V3.0 MuJoCo 模型:加载、单步推理与执行器响应解析
linux·运维·人工智能·python·深度学习·ubuntu·性能优化
yyuuuzz13 小时前
跨境站点日志时区混乱的排查记录
运维·服务器·网络·云计算·aws·1024程序员节
‎ദ്ദിᵔ.˛.ᵔ₎14 小时前
关于Linux 线程局部存储的认识
linux·运维
GlobalInfo14 小时前
全球及中国风电叶片检测机器人行业研究报告(2026版):市场现状、行业前景与占有率分析
运维·网络·人工智能·机器人
数字新视界15 小时前
一体化机柜在数据中心运维中的创新应用与优势分析
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
隔窗听雨眠15 小时前
KingbaseES平替全链路实践:从迁移评估到可持续运维
运维
skywalk816315 小时前
登到192.168.1.5那台机器,看看今天白天,大约从早晨到晚上,都卡住了。看看因为什么卡住,为什么卡住而没有杀死那个导致卡死的进程,为什么
运维·系统·freebsd