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

相关推荐
风哥2号19 小时前
数据库教程FGMT02‑生产环境Linux+Oracle19c安装配置与项目实战
linux·数据库·ffmpeg
wuminyu1 天前
Kafka中sendfile与mmap实现机制解析
java·linux·c语言·jvm·c++
新时代牛马1 天前
嵌入式网络完整篇:从LwIP/以太网驱动到 Linux netdev 与排障
linux·网络·php
猿与禅1 天前
Linux(Ubuntu)入门到实战:系统管理、常用命令与权限体系完整指南
linux·ubuntu·apt·进程管理·系统运维·用户权限·vi/vim
前端世界1 天前
Linux服务器实战:NTP时间同步、SELinux权限与rsyslog日志管理,一次搞懂三大运维问题
linux·运维·服务器
Android系统攻城狮1 天前
Linux Gstreamer深度解析之gst_audio_converter_new调用流程与实战(二十)
linux·运维·服务器·gstreamer音视频·音视频进阶
海宇服务1 天前
零信任架构实战:基于海宇对外投资历史查询服务构建自动化供应商准入网关
运维·人工智能·架构·自动化
上海云盾-小余1 天前
BGP 高防底层原理:TCP 异常流量识别与清洗机制
运维·网络·tcp/ip
captain3761 天前
▲网络原理(2)-TCP
java·服务器·网络·tcp/ip·java-ee
言乐61 天前
Python加速器4跨境网络加速器
运维·服务器·开发语言·网络·python