基于boost判断jpg文件结尾的2个字节是否正确,可以用来判断JPG文件是否完整。

本文提供了一段C++代码,通过读取JPEG图片文件尾部的两个字节来验证图片是否为JPEG格式。通过比较读取到的字节与JPEG文件的特定结束标记(ff0d),可以判断图片类型。此方法适用于处理和验证JPEG格式的图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * File:   main.cpp
 * Author: hcx
 *
 * Created on July 3, 2013, 4:04 PM
 */

#include <cstdlib>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/positioning.hpp>
#include <cstring>
#include <iostream>

using boost::iostreams::file_sink;
using boost::iostreams::file_source;
using boost::iostreams::position_to_offset;
using boost::iostreams::seek;
using boost::iostreams::stream_offset;
using namespace std;

/*
 * JPG 文件结尾的两个字节是 ff 0d。
 */
int main(int argc, char** argv) {
    
    char buf[3];    
    memset(buf, '\0', sizeof buf);
    file_source in("/home/hcx/pic/camera-vehicle/1g2pf413.4dk.slice.jpg", BOOST_IOS::binary);
    //------返回一个 stream_offset 表示与 pos 同一个字符位置,忽略任何代码转换状态。----------------//
    stream_offset off;
    off = position_to_offset(seek(in, -2, BOOST_IOS::end));
    //------------------------------//
    streamsize nr = in.read(buf, 2);
    short end0=(short)buf[0];
    short end1=(short)buf[1];
    if(end0==-1 && end1==-39)
          cout<<"This image is an jpg picture."<<endl;   
    else
          cout<<"This image is not  an jpg picture."<<endl;

    return 0;
}

可以使用Boost.PropertyTree库来解析JSON文件,并比较两个JSON文件的节点是否相同。以下是一个示例代码: ```cpp #include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> bool compareNodes(boost::property_tree::ptree& ptree1, boost::property_tree::ptree& ptree2) { if (ptree1.size() != ptree2.size()) { return false; } for (auto&& child1 : ptree1) { auto child2 = ptree2.get_child_optional(child1.first); if (!child2) { return false; } if (child1.second.data() != child2.get().data()) { return false; } if (!compareNodes(child1.second, child2.get())) { return false; } } return true; } int main() { boost::property_tree::ptree ptree1, ptree2; boost::property_tree::read_json("file1.json", ptree1); boost::property_tree::read_json("file2.json", ptree2); if (compareNodes(ptree1, ptree2)) { std::cout << "The two JSON files have the same nodes." << std::endl; } else { std::cout << "The two JSON files have different nodes." << std::endl; } return 0; } ``` 在上面的代码中,`compareNodes`函数递归比较两个`ptree`对象的所有子节点是否相同。如果两个`ptree`对象的子节点数不相同,则认为它们不相同。如果两个`ptree`对象有相同的子节点,但是它们的值不相同,则认为它们不相同。如果两个`ptree`对象的所有子节点都相同,则认为它们相同。在主函数中,我们使用`read_json`函数从文件中读取JSON数据,并调用`compareNodes`函数比较两个`ptree`对象是否相同。如果它们相同,则输出一条消息,否则输出另一条消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值