C++ 意外结果

C++ 意外结果,c++,C++,在我们的组织中,我们收到了以下格式的每日黑名单(更大,因为这只是一个片段): 172.44.12.0 198.168.1.5 10.10.0.0 192.168.78.6 192.168.22.22 111.111.0.0 222.222.0.0 12.12.12.12 当我在代码编译后运行程序时,我收到: 一, 一, 一, 一, 一, 一, 一, 一, 我在Linux/UNIX环境下使用C++。 到目前为止,我只是把它吐出来,以确保格式正确 #include <iostream>

在我们的组织中,我们收到了以下格式的每日黑名单(更大,因为这只是一个片段):

172.44.12.0

198.168.1.5

10.10.0.0

192.168.78.6

192.168.22.22

111.111.0.0

222.222.0.0

12.12.12.12

当我在代码编译后运行程序时,我收到:

一,

一,

一,

一,

一,

一,

一,

一,

我在Linux/UNIX环境下使用C++。 到目前为止,我只是把它吐出来,以确保格式正确

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <netinet/in.h>
#include <stdint.h>
#include <arpa/inet.h>

using namespace std;

bool is_match(std::string &hay_stack, std::string &srcip) {
    in_addr_t _ip = inet_addr(hay_stack.c_str());
    in_addr_t _IP = inet_addr(srcip.c_str());
    _ip = ntohl(_ip);
    _IP = ntohl(_IP);
    uint32_t mask=(_ip & 0x00ffffff == 0) ? 0xff000000 :
    (_ip & 0x0000ffff == 0 ? 0xffff0000 : 0);
    return ( (_ip & mask) == (_IP & mask) );
}

int main()
{
    vector<std::string> lines;
    lines.reserve(5000); //Assuming that the file to read can have max 5K lines

    string fileName("blacklist.txt");

    ifstream file;
    file.open(fileName.c_str());

    if(!file.is_open())
    {
        cerr<<"Error opening file : "<<fileName.c_str()<<endl;
        return -1;
    }

    //Read the lines and store it in the vector
    string line;
    while(getline(file,line))
    {
        lines.push_back(line);
    }

    file.close();

    //Dump all the lines in output
    for(unsigned int i = 0; i < lines.size(); i++)
    {
        string h = lines[i];
        string mi = "10.10.10.10";
        cout<<is_match(h,mi)<<endl;
    }

    return 0;
}
文件名为blacklist.txt,其中包含以上列出的IP。我只是使用cout来确保变量定义正确

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <netinet/in.h>
#include <stdint.h>
#include <arpa/inet.h>

using namespace std;

bool is_match(std::string &hay_stack, std::string &srcip) {
    in_addr_t _ip = inet_addr(hay_stack.c_str());
    in_addr_t _IP = inet_addr(srcip.c_str());
    _ip = ntohl(_ip);
    _IP = ntohl(_IP);
    uint32_t mask=(_ip & 0x00ffffff == 0) ? 0xff000000 :
    (_ip & 0x0000ffff == 0 ? 0xffff0000 : 0);
    return ( (_ip & mask) == (_IP & mask) );
}

int main()
{
    vector<std::string> lines;
    lines.reserve(5000); //Assuming that the file to read can have max 5K lines

    string fileName("blacklist.txt");

    ifstream file;
    file.open(fileName.c_str());

    if(!file.is_open())
    {
        cerr<<"Error opening file : "<<fileName.c_str()<<endl;
        return -1;
    }

    //Read the lines and store it in the vector
    string line;
    while(getline(file,line))
    {
        lines.push_back(line);
    }

    file.close();

    //Dump all the lines in output
    for(unsigned int i = 0; i < lines.size(); i++)
    {
        string h = lines[i];
        string mi = "10.10.10.10";
        cout<<is_match(h,mi)<<endl;
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
bool是匹配的(std::string和hay_堆栈,std::string和srcip){
in_addr_t_ip=inet_addr(hay_stack.c_str());
in_addr_t_IP=inet_addr(srcip.c_str());
_ip=ntohl(_-ip);
_IP=ntohl(_-IP);
uint32\u t掩码=(\u ip&0x00ffffff==0)?0xff000000:
(ip&0x0000ffff==0?0xffff0000:0);
返回((ip和掩码)=((ip和掩码));
}
int main()
{
矢量线;
lines.reserve(5000);//假设要读取的文件最多可以有5K行
字符串文件名(“blacklist.txt”);
ifstream文件;
打开(fileName.c_str());
如果(!file.is_open())
{
cerr回答了一个隐含的问题,“为什么我的程序不能按我期望的方式工作?”

我希望输出为10.10.10.10(此处为某种主机子网)10.10.0.0(此处为某种子网掩码)

我不知道您为什么希望这样。您的代码(如果文件成功打开)中只有一条打印语句:

cout<<is_match(h,mi)<<endl;

cout这就是您的问题所在:

uint32_t mask=(_ip & 0x00ffffff == 0) ? 0xff000000 :
(_ip & 0x0000ffff == 0 ? 0xffff0000 : 0);
return ( (_ip & mask) == (_IP & mask) );
如果_ip的形式为x.0.0.0,则它仅比较x in _ip, 如果ip的形式是x.y.0.0,它只比较ip中的x和y, 这很好


但是,如果_ip不是任何一种格式,您将掩码设置为0,我猜“掩码”的计算结果始终为零。您是否有特定的问题,或者我们是否应该为您修复程序?即使您没有提供问题,我仍将+1用于:提供完整的程序()并且使用正确的习惯用法来读取输入行。由于布尔值总是正确的……无论他在
中尝试做什么,都是_match()
…不起作用。我正在尝试获取10.10.10.10,看看它是否与blacklist.txt中的任何内容匹配。由于属于网络范围的条目不包含子网掩码,我假设我必须为这些地址分配一个掩码,看看我的主机是否在该范围内。我想我刚刚得到它。更改==感谢我对progra很陌生mming,到目前为止,我基本上只花了几个小时学习C++语言。多亏我是编程新手,我现在只收到了所有零。我是否应该为匹配返回一个不同的值(10.10.10.10驻留在10.10.0.0子网中)因此,我希望得到与blacklist.txt文件中包含的其他值不同的结果?结果发现存在一个更微妙的操作优先级问题。我更新了答案来解释它。
uint32_t mask=( (_ip & 0x00ffffff) == 0) ? 0xff000000 :
    ( (_ip & 0x0000ffff) == 0 ? 0xffff0000 : 0xffffffff);
return ( (_ip & mask) == (_IP & mask) );