Need help in Parsing of file in C/C++ !!

Discussion in 'C' started by maverickjd, Sep 11, 2013.

  1. maverickjd

    maverickjd New Member

    Joined:
    Sep 11, 2013
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I have to write a C/C++ code which can read a text file with the following contents :

    Device ID no of ALIVE No of SEARCH no of BYEBYE
    uuid:22314754-a597-490b-8a93-02cfae01036b ALIVE 16
    uuid:22314754-a597-490b-8a93-02cfae01036b SEARCH 8
    uuid:2ad9982a-c1ee-4037-8029-90b00c89a368 ALIVE 13
    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 SEARCH 111
    uuid:55076f6e-6b79-4d65-6497-180373763bc1 BYEBYE 112


    And format it such that the output file will be printed like below :

    Device ID no of ALIVE No of SEARCH no of BYEBYE
    uuid:22314754-a597-490b-8a93-02cfae01036b 16 8 0
    uuid:2ad9982a-c1ee-4037-8029-90b00c89a368 13 0 0
    uuid:50e65653-7525-485d-83bf-d293558c4264 32 0 8
    uuid:55076f6e-6b79-4d65-6497-180373763bc1 113 111 112

    The above is just an example ,basically the Device id will be getting populated dynamically, so each time,the "uuid:" string name and their occurrences(ALIVE/SEARCH/BYEBYE) might be different,so how to format it in such a way that I can track reading from any input file (with different uuid string name and different occurences) and print to an output file format,Any ideas?

    Regards
    Debasis
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. maverickjd

    maverickjd New Member

    Joined:
    Sep 11, 2013
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Shabbir.I was just having a look.I have not checked it.Its not in c++
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Ohh then what is that?
     
  5. maverickjd

    maverickjd New Member

    Joined:
    Sep 11, 2013
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    My compiler is g++ (ubuntu ver 10.04) .Does not support window libraries.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    So show some effort of what you have done and will be more than happy to help you solve the issue you have in your code.
     
  7. maverickjd

    maverickjd New Member

    Joined:
    Sep 11, 2013
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    So I am now 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 :
    HTML:
    #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);
    //              i++;
            }
            else
            {
    
            }
    
            temp_val[0] = '\0';
    
            strcpy(temp_val,UID[i].uid_val);
    //      printf("value is %s and %s and ret is %d\n",temp_val,UID[i].uid_val,ret);
    
            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;
            }
    
            //printf("UID is %s  State is %s  Count is %d\n",UID[i].uid_val,UID[i].state,UID[i].alive_count);
            line++;
    
            if(line%3 == 0)
            {
                    i++;
            }
    }
    
    //printf("value of is %d and lines is %d\n",i,line);
    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);
    }
    
    
    }
    
    //      for(n = 0;n < i;n++)
            {
    //              printf("%s    %d %d  %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 crashes 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.PL let me know what to do.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Your code is not C++ either. It is all plain C BTW and instead of doing the way you are doing why don't you read the complete line of text aka

    uuid:55076f6e-6b79-4d65-6497-180373763bc1 ALIVE 113

    And then do a split based on space character and then add them to the structure.

    Which would give you

    uuid:55076f6e-6b79-4d65-6497-180373763bc1
    ALIVE
    113

    And it should also work for all.
     
  9. maverickjd

    maverickjd New Member

    Joined:
    Sep 11, 2013
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks.It can be moves in C forum.

    There is a mistake..Code does not crash, but gives wrong results.. apologies..Thanks
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What result do you expect and what result does it give.

    The process I suggested would make your code general and work for any kind of input
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice