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

相关推荐
nazisami3 分钟前
程序地址空间
linux·虚拟地址
库拉镜像AI牛牛17 分钟前
漫剧工作室量产方案:依托知漫剧 AI 短剧降本增效
大数据·服务器·前端·人工智能·语音识别
不会就选b27 分钟前
Linux之TCP<2>
linux·网络·tcp/ip
DP DPharness29 分钟前
浏览器自动化插件时,三个可核对的判断维度
运维·自动化
沫璃染墨1 小时前
《从零入门Linux系统篇(五十八):线程篇·十一——线程安全与死锁详解:从可重入到多锁管理》
linux·服务器·开发语言·c++·驱动开发·安全·架构
姜鱼问生1 小时前
数据库备份 + 灾难恢复演练:从 dump 恢复全库
运维·数据库
姜鱼问生1 小时前
宝塔面板优雅下线:先 stop 再 disable 再 rm
运维·服务器
墨家句子2 小时前
4G 显存老显卡怎么跑模型:llama.cpp 加 GGUF 配置
linux·后端
闭包不眠2 小时前
PDF文字提取交付前该检查什么
运维·服务器·图像处理·人工智能·计算机视觉·pdf
吴声子夜歌2 小时前
Nginx应用与运维——Nginx在Kubernetes中的应用(一)
运维·nginx·kubernetes