1

这适用于初学者的 C 编程单元。我正在尝试读取包含 MAC 地址及其接收到的数据的文本文件,分离出相关数据(地址和数据包数量),将地址复制到数组而不重复其中任何一个,并将相关的数据包数量相加,如果遇到相同的地址。

我可以很好地读取文件,并且可以毫无问题地获取我想要的每一行的位,但是当我尝试检查每个读取的地址与数组中已经存在的地址时,我遇到了问题。根据计算整行数的整数的位置,程序要么无法识别相同的字符串并将它们全部打印在文件中,要么在地址[0]中将它们相互打印,只留下我最后地址。我很难过,需要一些新的眼光 - 任何建议将不胜感激。

我的代码如下:

static void readadds(char filename[])
{
FILE* packetfile = fopen(filename, "r");
FILE* datafile = fopen("packdata.txt", "w+");
// Open file from input; create temporary file to store sorted data.

char line[100];
char addresses[500][18];
int datasize[500];
int addressno = 0;
// Create storage for lines read from text file, addresses and related data.

if(packetfile != NULL)
{
    while(fgets(line, sizeof line, packetfile) != NULL)
    {
        int linenum = 0;
        char thisadd[18];
        int thisdata;
        //Create arrays to temp store data from each line

        sscanf(line, "%*s   %*s %s  %i", thisadd, &thisdata);

        for(int i = 0; i < 500; i++)
        {
            if(strcmp(thisadd, addresses[i]) == 0)
            {   //check if the address is already in the array 
                int x = datasize[i];
                datasize[i] = x + thisdata; //sum packet data if address already exists
                printf("Match!\n");
                break; 
            }
            else
            {
                strcpy(addresses[linenum], thisadd); //initialize new address
                datasize[linenum] = thisdata; //initialize assoc. data
                linenum++;
                addressno++;
                printf("Started!\n");
                break;
            }
        }
    }
    for(int i = 0; i <= addressno; i++)
        {
            printf("%s  %i\n", addresses[i], datasize[i]);
            fprintf(datafile,"%s    %i\n", addresses[i], datasize[i]);
        }
}
fclose(packetfile);
fclose(datafile);
}

此版本在地址 [0] 上打印。如果在 for() 循环中将 linenum 替换为 addressno,则无法识别相同的字符串。我的数据集是这样排列的:

1378251369.691375   84:1b:5e:a8:bf:7f   68:94:23:4b:e8:35   100
1378251374.195670   00:8e:f2:c0:13:cc   00:11:d9:20:aa:4e   397
1378251374.205047   00:8e:f2:c0:13:cc   00:11:d9:20:aa:4e   397
1378251374.551604   00:8e:f2:c0:13:cc   00:11:d9:20:aa:4e   157
1378251375.551618   84:1b:5e:a8:bf:7c   cc:3a:61:df:4b:61   37
1378251375.552697   84:1b:5e:a8:bf:7c   cc:3a:61:df:4b:61   37
1378251375.553957   84:1b:5e:a8:bf:7c   cc:3a:61:df:4b:61   37
1378251375.555332   84:1b:5e:a8:bf:7c   cc:3a:61:df:4b:61   37
4

1 回答 1

1

几乎可以肯定这就是你想要做的。添加新条目的逻辑不正确。如果你已经用尽了所有当前的搜索,你只添加一个,这意味着你需要在添加之前完成当前的 for-search。

注意:未经编译测试,但希望您能理解。

static void readadds(char filename[])
{
    // Open file from input; create temporary file to store sorted data.
    FILE* packetfile = fopen(filename, "r");
    FILE* datafile = fopen("packdata.txt", "w+");

    // Create storage for lines read from text file, addresses and related data.
    char addresses[500][18];
    int datasize[500];
    int addressno = 0;

    if (packetfile != NULL)
    {
        char line[100];
        while(fgets(line, sizeof line, packetfile) != NULL)
        {
            char thisadd[18];
            int thisdata = 0;

            //Create arrays to temp store data from each line
            if (sscanf(line, "%*s   %*s %s  %i", thisadd, &thisdata) == 2)
            {
                // try to find matching address
                for(int i = 0; i < addressno; i++)
                {
                    if(strcmp(thisadd, addresses[i]) == 0)
                    {
                        //check if the address is already in the array
                        datasize[i] += thisdata;;
                        printf("Match!\n");
                        break;
                    }
                }

                // reaching addressno means no match. so add it.
                if (i == addressno)
                {
                    printf("Started!\n");
                    strcpy(addresses[addressno], thisadd); //initialize new address
                    datasize[addressno++] = thisdata; //initialize assoc. data
                }
            }
            else
            {   // failed to parse input parameters.
                break;
            }
        }

        for(int i = 0; i <= addressno; i++)
        {
            printf("%s  %i\n", addresses[i], datasize[i]);
            fprintf(datafile,"%s    %i\n", addresses[i], datasize[i]);
        }
    }
    fclose(packetfile);
    fclose(datafile);
}
于 2013-09-17T06:36:40.953 回答