我想实现一个统计TXT文件中不同IP地址的功能,就是按键后,可以输出存在TXT文件中那写IP地址各有几个,比如txt文件中的数据是:
192.168.1.1
192.168.1.5
192.168.1.8
192.168.1.18
192.168.1.1
192.168.1.8
192.168.1.18
可以输出192.168.1.1——2个
192.168.1.5——1个
192.168.1.8——2个
192.168.1.18——2个
有没有高手知道呀?

解决方案 »

  1.   

    ifstream infile("IP.txt");
    map<string, int> mapIP;
    string sLine;
    while(getline(infile, sLine))
    {
      mapIP[sLine]++;
    }
      

  2.   

    1.每次读入一行,用 inet_addr转化为long型数据;
    2.对long型数据进行比较,相同的分为一组,分组统计个数就可以了
      

  3.   

    //建议看看C++ primer 4#include <Windows.h>
    #include <tlhelp32.h>
    #include <stdio.h>
    #include <string>
    #include <tchar.h>
    #include <io.h>
    #include <map>using namespace std;
    BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map);
    int _tmain(int argc, TCHAR* argv[])
    {
    map< string, int> IPCount_map;
    string strFilePath = "F:\\aaa.txt";
    ReadTxt(strFilePath, IPCount_map);
    for(map< string, int>::iterator iter = IPCount_map.begin(); iter != IPCount_map.end(); ++iter)//遍历
    {
    printf("%s ---- %d个\n", iter->first.c_str(), iter->second);
    }
    return 0;
    }BOOL ReadTxt(string strFilePath,  map< string, int> &IPCount_map)
    {
    char buf[100] = {0};
    int i = 0, ch = 0;
    errno_t err;
    FILE *stream = NULL;
    if( (err = fopen_s(&stream, strFilePath.c_str(), "r")) != 0 )
    {
    printf("ERROR: Open or read %s is Error\n", strFilePath.c_str());
    return FALSE;
    }
    ch = fgetc( stream );
    while(feof( stream ) == 0)//读取每行数据
    {
    buf[i] = (char)ch;
    if(buf[i] == '\n')
    {
    buf[i] = '\0';
    string strLine = buf;
    IPCount_map[strLine]++; //IP计数
    i = 0;
    }
    ch = fgetc( stream );
    i++;
    }
    fclose( stream );
    return TRUE;
    }
      

  4.   

    void CNBTSTATDlg::OnBnClickedStatis()
    {
        BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map);
        map< string, int> IPCount_map;
        string strFilePath = "F:\\aaa.txt";
        ReadTxt(strFilePath, IPCount_map);
        for(map< string, int>::iterator iter = IPCount_map.begin(); iter != IPCount_map.end(); ++iter)//遍历
        {
            printf("%s ---- %d个\n", iter->first.c_str(), iter->second);
        }
        return 0;
    BOOL ReadTxt(string strFilePath,  map< string, int> &IPCount_map)
    {
        char buf[100] = {0};
        int i = 0, ch = 0;
        errno_t err;
        FILE *stream = NULL;
        if( (err = fopen_s(&stream, strFilePath.c_str(), "r")) != 0 )
        {
            printf("ERROR: Open or read %s is Error\n", strFilePath.c_str());
            return FALSE;
        }
        ch = fgetc( stream );
        while(feof( stream ) == 0)//读取每行数据
        {
            buf[i] = (char)ch;
            if(buf[i] == '\n')
            {
                buf[i] = '\0';
                string strLine = buf;
                IPCount_map[strLine]++;    //IP计数
                i = 0;
            }
            ch = fgetc( stream );
            i++;
        }
        fclose( stream );
        return TRUE;
    }}    我把代码加到按钮下,但是报错了
    错误 7 error C2562: “CNBTSTATDlg::OnBnClickedStatis”: “void”函数返回值 678 Graduation project
    错误 8 error C2601: “ReadTxt”: 本地函数定义是非法的 682 Graduation project
      

  5.   

    ....
    同学,你不要照着抄。MFC的函数不是这样加的,你要在CNBTSTATDlg类内部声明BOOL ReadTxt(string strFilePath, map< string, int> &IPCount_map);在.cpp中加上定义。然后在OnBnClickedStatis调用。
      

  6.   

    我想点一个按键,然后在list输出结果,请问应该怎么改?