0

I am working on a project where I have to capture statistics of uuid occurrences for ALIVE/SEARCH/BYEBYE (it can be all 3, combinations of 2 each, or one alone) in a dynamically populated file in run time.

I am able to print all 3 combinations, but not in combination of 1 or 2 e.g.

If my input.txt is like this :

uuid:22314754-a597-490b-8a93-02cfae01036b ALIVE 16
uuid:22314754-a597-490b-8a93-02cfae01036b BYEBYE 8
uuid:22314754-a597-490b-8a93-02cfae01036b SEARCH 8
uuid:50e65653-7525-485d-83bf-d293558c4264 ALIVE 32
uuid:50e65653-7525-485d-83bf-d293558c4264 BYEBYE 8
uuid:50e65653-7525-485d-83bf-d293558c4264 SEARCH 132
uuid:55076f6e-6b79-4d65-6497-180373763bc1 ALIVE 113
uuid:55076f6e-6b79-4d65-6497-180373763bc1 BYEBYE 112
uuid:55076f6e-6b79-4d65-6497-180373763bc1 SEARCH 111
uuid:T0100203354 ALIVE 1
uuid:T0100203354 BYEBYE 2
uuid:T0100203354 SEARCH 3

My code :

#include<stdio.h>
#include<string.h>

struct uid
{
    char uid_val[100];
    char state[100];
    int temp_count;
    int alive_count;
    int search_count;
    int bye_count;

} UID[100];

int main()
{
    char str[100];
    int i = 0;

    int line = 0;

    char temp_val[100] = "waseem";

    FILE *fp1 = fopen("input.txt","r");
    FILE *fp2=fopen("output.txt","w");

    printf("init value is %s \n",temp_val);
    while(!feof(fp1))
    {
        fscanf(fp1,"%s %s %d",UID[i].uid_val,UID[i].state,&UID[i].temp_count);

        int ret = 0;
        ret=strcmp(UID[i].uid_val,temp_val);
        if (ret!=0)
        {
            printf("new UID_val is %s\n",UID[i].uid_val);

        }
        else
        {
        }

        temp_val[0] = '\0';
        strcpy(temp_val,UID[i].uid_val);


        if(strcmp(UID[i].state,"ALIVE")==0)
        {
            UID[i].alive_count = UID[i].temp_count;
        }
        else if(strcmp(UID[i].state,"BYEBYE")==0)
        {
            UID[i].search_count = UID[i].temp_count;
        }
        else
        {
            UID[i].bye_count = UID[i].temp_count;
        }


        line++;

        if(line%3 == 0)
        {
            i++;
        }
    }


    int n = 0;
    //fp2=fopen("output.txt","w");

    if (fp2==NULL)
    {
        printf("cant output to file\n");
    }
    else
    {
        fprintf(fp2,"Device ID\t\t\t\t\tALIVE\tBYEBYE\tSEARCH\n");
        for(n = 0;n < i;n++)
        {
            fprintf(fp2,"%s\t%d\t%d\t%d\n",UID[n].uid_val,UID[n].alive_count,UID[n].search_count,UID[n].bye_count);
        }
    }




        fclose(fp1);
        fclose (fp2);

        return 0;
    }
}

Gives the following output :(output.txt)

Device ID ALIVE BYEBYE SEARCH
uuid:22314754-a597-490b-8a93-02cfae01036b 16 8 8
uuid:50e65653-7525-485d-83bf-d293558c4264 32 8 132
uuid:55076f6e-6b79-4d65-6497-180373763bc1 113 112 111
uuid:T0100203354 1 2 3

I want to generalize the code such that uuid occurrence does not have to be all 3 (ALIVE/SEARCH/BYEBYE), the occurrences can be any combination and code should work. e.g my code gives wrong results when input.txt contains the following:

uuid:22314754-a597-490b-8a93-02cfae01036b BYEBYE 8
uuid:22314754-a597-490b-8a93-02cfae01036b SEARCH 8
uuid:50e65653-7525-485d-83bf-d293558c4264 ALIVE 32
uuid:50e65653-7525-485d-83bf-d293558c4264 BYEBYE 8
uuid:55076f6e-6b79-4d65-6497-180373763bc1 ALIVE 113
uuid:55076f6e-6b79-4d65-6497-180373763bc1 BYEBYE 112
uuid:55076f6e-6b79-4d65-6497-180373763bc1 SEARCH 111
uuid:T0100203354 BYEBYE 2

I am using ubuntu for gcc/g+ compiler.

  • use `gdb` to debug your program. – PersianGulf Sep 13 '13 at 05:23
  • Please indent your code and avoid commented code in question, it confuses. – Grijesh Chauhan Sep 13 '13 at 05:24
  • Removed the `c++` tag as this is pure C. – Jesse Good Sep 13 '13 at 05:27
  • I find it interesting you think it important enough to check the result of `feof()` [(which is wrong, by the way)](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong), but completely ignore the return results of two `fopen()` calls and a `fscanf()` call. *Check your api return values. They're there for a reason.* – WhozCraig Sep 13 '13 at 05:36

3 Answers3

0

As you are reading a line from file and processing it, Why not tokenlze the line using strtok using UUID? You will get multiple UUID's for each line processed.

Later, you need to sort these tokenized lines accordingly, so that you get all occurances of a UUID at one place. Now print the status of all tokens in order. Thats it. !!

kris123456
  • 501
  • 1
  • 5
  • 15
  • There is a mistake..Code does not crash, but gives wrong results.. apologies..removes the commented lines as well for better understanding..Thanks – user2768747 Sep 13 '13 at 05:52
  • If the results are wrong, definitely your code is wrong. You need to correct it. – kris123456 Sep 17 '13 at 06:10
  • Refer to my new answer below. Mods: Deleting this comment and adding it as an answer as my description is exceeding its length. – kris123456 Sep 20 '13 at 16:36
0

Here is what I would suggest you. Have a different structure to store the output which would reduce complexity in your loop. Check your uid with the previous uid received, and assign to output structure accordingly. you better check the code I rewrote. It works for both the output

#include<stdio.h>
#include<string.h>

struct uid
{
    char uid_val[100];
    char state[100];
    int temp_count;

} UID[100];

struct uid1
{
    char uid_val[100];
    int alive_count;
    int search_count;
    int bye_count;

} UID_NEW[100];

int main()
{
    char str[100];
    int i = 0;
    int j=0;
    FILE *fp1 = fopen("input.txt","r");
    FILE *fp2=fopen("output.txt","w");
    while(!feof(fp1))
    {
        fscanf(fp1,"%s %s %d",UID[i].uid_val,UID[i].state,&UID[i].temp_count);
        if(i>0)
        {
            if(strcmp(UID_NEW[j].uid_val,UID[i].uid_val)!=0)
            {
                j++;
            }
        }
        strcpy(UID_NEW[j].uid_val,UID[i].uid_val);

        if(strcmp(UID[i].state,"ALIVE")==0)
        {
            UID_NEW[j].alive_count = UID[i].temp_count;
        }
        else if(strcmp(UID[i].state,"BYEBYE")==0)
        {
            UID_NEW[j].search_count = UID[i].temp_count;
        }
        else
        {
            UID_NEW[j].bye_count = UID[i].temp_count;
        }

        i++;
    }
    int n = 0;
    if (fp2==NULL)
    {
        printf("cant output to file\n");
    }
    else
    {
        fprintf(fp2,"Device ID\t\t\t\t\tALIVE\tBYEBYE\tSEARCH\n");
        for(n = 0;n <= j;n++)
        {
            fprintf(fp2,"%50s\t%3d\t%3d\t%3d\n",UID_NEW[n].uid_val,UID_NEW[n].alive_count,UID_NEW[n].search_count,UID_NEW[n].bye_count);
        }
    }
    fclose(fp1);
    fclose (fp2);
    return 0;
}
Pritesh Acharya
  • 1,596
  • 5
  • 15
  • 36
  • Thanks a lot Pritesh..It is working..Just that I found an extra line with "0 0 0" at the end of the output file.Will check it out. – user2768747 Sep 13 '13 at 09:35
  • Sure thing.Your idea of having a second structure to store the output worked!!I am new to this forum.Where should I mark your answer as accepted ? – user2768747 Sep 13 '13 at 10:05
0

I am sorry to say this, but this is one simple task, which you should be able to fix the code.Refer the below URL which explains how to read last word of a string.

C++ find return the last word in the string variable text. string getFirstWord(text) The reason for providing this url being 1) If you observe clearly, your input file has line like "UUID:asdfsdfsdf Name...." The word followed by first space is what you want. So you need to extract the second word from the line read from file 2) Now you need to sort the contents of the tokens parsed. This is another simple task. Use an appropriate container. 3) Perform a sort and display the result. Now Try and update us with your results.

Community
  • 1
  • 1
kris123456
  • 501
  • 1
  • 5
  • 15