how to compare string value in C (strcmp)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • need some help
    New Member
    • Apr 2007
    • 12

    how to compare string value in C (strcmp)

    Hi,

    Can you please help me...I am not able solve this issue. I have a array AttValName[Att][v] which has following values,
    Att=Outlook,

    AttValName[Att][1]=sunny
    AttValName[Att][2]=Rain
    AttValName[Att][3]=overcast

    and data is like,

    1.sunny,hot,mil d,play
    2. sunny,normal,hi gh,Don'y play
    3....
    4...

    and I need to get record number when AttValName[Att][v] = "sunny"
    Is is possible to do that? If yes, Can you please tell me how?

    Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use the strcmp() function to compare C strings. Check your docset.

    You may have other problems based on your code snippet. Keep us advised how you are progressing.

    Comment

    • need some help
      New Member
      • Apr 2007
      • 12

      #3
      I understand I have to use strcmp() but, how do I retrive each record from the file ..
      here at the place of ??? how do I get my string from the file and its record no???
      in C.

      strcmp(AttValNa me[Att][v]," ???")
      1. sunny, high,low, play


      thanks,



      Originally posted by weaknessforcats
      Use the strcmp() function to compare C strings. Check your docset.

      You may have other problems based on your code snippet. Keep us advised how you are progressing.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You will have to read the disc file into arrays. You show data like:

        sunny, high,low, play


        You can read this data into a buffer you allocate. The buffer would contain the data from one record. Maybe:

        [CODE=cpp]
        char* myRecords[10]; //enough for 10 disc records

        //Allocate memory for a buffer that can hold data for one record:
        char* buffer = malloc(a large enough number for 1 disc record);
        //Read the disc into the buffer.
        --how to do this omitted ---
        //Then put the array in the myRecords array
        myRecords[i] = buffer;
        [/CODE]

        Your record numbers are now the subscripts of the myRecords array. All of the values:

        sunny, high,low, play

        are in one string. You will then process the string to see the values. Here's where the strcmp() will come in.

        Comment

        • need some help
          New Member
          • Apr 2007
          • 12

          #5
          Hi

          Thanks for the help. When I will get whole string in myrecord[] then to get only "sunny" from that string do I have to use strtok() ? because I just want to compare AttvalName[Att][v] with only sunny not the whole line.


          Originally posted by weaknessforcats
          You will have to read the disc file into arrays. You show data like:

          sunny, high,low, play


          You can read this data into a buffer you allocate. The buffer would contain the data from one record. Maybe:

          [CODE=cpp]
          char* myRecords[10]; //enough for 10 disc records

          //Allocate memory for a buffer that can hold data for one record:
          char* buffer = malloc(a large enough number for 1 disc record);
          //Read the disc into the buffer.
          --how to do this omitted ---
          //Then put the array in the myRecords array
          myRecords[i] = buffer;
          [/CODE]

          Your record numbers are now the subscripts of the myRecords array. All of the values:

          sunny, high,low, play

          are in one string. You will then process the string to see the values. Here's where the strcmp() will come in.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by need some help
            Hi

            Thanks for the help. When I will get whole string in myrecord[] then to get only "sunny" from that string do I have to use strtok() ? because I just want to compare AttvalName[Att][v] with only sunny not the whole line.
            That is certainly one way of doing it. Try looking up the str functions and you will find some functions to search a string for a substring.


            Adrian

            Comment

            • need some help
              New Member
              • Apr 2007
              • 12

              #7
              To get the data as you said I am writing following code but its not working. Can you please tell me where I am wronge?


              [code=c]#include <stdio.h>
              #include <string.h>

              main()
              {
              char Fn[100];
              FILE *Nf;
              char *buffer[100];
              char *myrecord[10];
              int i=0,x=0;

              // strcpy(Fn,wthr) ;
              // strcat(Fn,".dat a");
              // printf("file name is : %s \n",FileName) ;

              Nf=fopen("wthr. data","r");

              fread(buffer,0, 100,Nf);
              myrecord[i] = buffer[i];
              i++;

              while(!feof(Nf) )
              {
              fscanf(Nf,"%f ", &(myrecord[x]));
              x++;

              }
              fclose(Nf);
              }[/code]

              Thanks.


              Originally posted by AdrianH
              That is certainly one way of doing it. Try looking up the str functions and you will find some functions to search a string for a substring.


              Adrian
              Last edited by AdrianH; May 26 '07, 07:50 PM. Reason: Please use [code=c][/code] tags for readability.

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by need some help
                To get the data as you said I am writing following code but its not working. Can you please tell me where I am wronge?


                [code=c]#include <stdio.h>
                #include <string.h>

                main()
                {
                char Fn[100];
                FILE *Nf;
                char *buffer[100];
                char *myrecord[10];
                int i=0,x=0;

                // strcpy(Fn,wthr) ;
                // strcat(Fn,".dat a");
                // printf("file name is : %s \n",FileName) ;

                Nf=fopen("wthr. data","r");

                fread(buffer,0, 100,Nf);
                myrecord[i] = buffer[i];
                i++;

                while(!feof(Nf) )
                {
                fscanf(Nf,"%f ", &(myrecord[x]));
                x++;

                }
                fclose(Nf);
                }[/code]

                Thanks.
                You have read in a big chunk of code using fread(), then you try and read in more data using fscanf(). Why? What is your intent?


                Adrian

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by AdrianH
                  You have read in a big chunk of code using fread(), then you try and read in more data using fscanf(). Why? What is your intent?


                  Adrian
                  Yes,and what is BTW purpose of setting size of each data item to 0?

                  fread(buffer,0//Size of item which is been readed n times,100//n,Nf);

                  Savage

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by Savage
                    Yes,and what is BTW purpose of setting size of each data item to 0?

                    fread(buffer,0//Size of item which is been readed n times,100//n,Nf);

                    Savage
                    Doh! Missed that one. ;) You aren't reading in anything since 0*anything is 0.


                    Adrian

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by AdrianH
                      Doh! Missed that one. ;) You aren't reading in anything since 0*anything is 0.


                      Adrian
                      Don't worry,it can happen to anyone.

                      :)

                      Savage

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by Savage
                        Don't worry,it can happen to anyone.

                        :)

                        Savage
                        Not me. :cry: I'm loosing my touch. :bawl: LOL JK


                        Adrian

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by AdrianH
                          Not me. :cry: I'm loosing my touch. :bawl: LOL JK


                          Adrian
                          LOL!

                          Savage

                          Comment

                          • need some help
                            New Member
                            • Apr 2007
                            • 12

                            #14
                            [code=c]#include<stdio. h>
                            main()
                            {

                            char buffer[100];
                            // char myrec[100];
                            FILE *Nf;
                            char *c;
                            char *myrec[10];
                            int i = 0, j;

                            Nf = fopen("wthr.dat a", "r");

                            do {
                            c = fgets(buffer, 100, Nf);
                            myrec[i] = buffer;
                            printf("myrecor d is : %s \n", myrec[i]);
                            i = i + 1;

                            if (c != NULL)
                            printf("%s", buffer);
                            } while (c != NULL);

                            for (j = 0; j < 10; j++) {
                            printf("records are : %s \n", myrec[j]);
                            }

                            fclose(Nf);

                            }[/code]

                            now its printing with first 2 printfs but why its not printing at printf in for loop?


                            Originally posted by Savage
                            LOL!

                            Savage
                            Last edited by AdrianH; May 27 '07, 02:20 PM. Reason: Please use [code=c][/code] tags for improved readability. Also, DO CONSISTENT INDENTING!

                            Comment

                            • Savage
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1759

                              #15
                              Originally posted by need some help[CODE=c
                              ]#include<stdio. h>
                              main()
                              {

                              char buffer[100];
                              // char myrec[100];
                              FILE *Nf;
                              char *c;
                              char* myrec[10];
                              int i=0,j;

                              Nf = fopen("wthr.dat a", "r");

                              do
                              {
                              c= fgets(buffer,10 0,Nf);
                              myrec[i] = buffer;
                              printf("myrecor d is : %s \n",myrec[i]);
                              i=i+1;

                              if (c != NULL)
                              printf("%s", buffer);
                              } while (c != NULL);

                              for(j=0;j<10;j+ +)
                              {
                              printf("records are : %s \n",myrec[j]);
                              }

                              fclose(Nf);

                              }[/CODE]
                              now its printing with first 2 printfs but why its not printing at printf in for loop?
                              How do u mean not printing?

                              BTW,ur do-while loop is not completly good designed.It should increase i only if c!=NULL,and it should break before inputing data from buffer to myrec if c==NULL,so it should look like:

                              [CODE=c]do{
                              if((c=fgets(buf fer,100,Nf))=NU LL) break;
                              myrec[i]=buffer;
                              //print record
                              if(c!=NULL) //print buffer;

                              }while(1);[/CODE]

                              Savage

                              Comment

                              Working...