سؤال

This is for a beginner's C programming unit. I'm trying to read a text file containing MAC addresses and the data they received, separate out the relevant data (address and number of packets), copy the addresses to an array without repeating any of them and sum the associated number of packets if an identical address is encountered.

I can read the file in just fine, and get the bits of each line I want without issue, but when I try to check each address read against those already in the array I hit a problem. Depending on the location of the integer counting the number of full lines, the program either fails to recognise identical strings and prints them all as they are in the file, or prints them over one another in addresses[0], leaving me with only the last address. I'm stumped and need some fresh eyes on this - any suggestions would be greatly appreciated.

My code follows:

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);
}

This version prints over addresses[0]. If linenum is replaced by addressno in the for() loop, identical strings are not recognised. My dataset is arranged like this:

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
هل كانت مفيدة؟

المحلول

I'm almost certain this is what you're trying to do. The logic to add a new entry was incorrect. You only add one if you have exhausted searching all the current ones, which means you need to finish the current for-search before the add.

Note: Not tested for compilation, but hopefully you get the idea.

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top