Thread: strcmp error

  1. #16
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Code:
    while(fgets(line,Char,que)!=NULL && strcmp(line,"quit") !=0)//when the command is not eof
        {
          strcpy(curline,line);
          //      printf("curline= %s\n",curline);
          token1=strtok(line," \n\t");//get the command
          while(token1 != NULL)//when command is not quit
            {
              if(strcmp("count",token1) ==0)//when command is count
                {
                  printf("calling count\n");
                  fflush(stdout);
                  count();//call count
                  printf("called count\n");
                  fflush(stdout);
                }
    Code:
    void count(void)//the founction print out the number of relations
    {
      char * token1;
      //  char * attname;
      char * str;
      int size;
      int x =0;
      int right =0;
      printf("curline in count  = %s\n", curline);
      token1 = strtok(curline, " \r\t\n");//use strtok to get the first word
      while(token1 != NULL)
        {
          printf("token1 %s\n",token1);
          token1 = strtok (NULL, " \r\t\n");//continue to part the string
        }
      printf("end of token\n");
      size =tableSize;//set size
      printf("size = %d\n",size);
      while(size>0 && right == 0)//if size is>0 and right is 0
        {
          str = relation[x].name;
          printf("str= %s\n",str);
          printf("token1= %s\n",token1);
          printf("1");
          if((strcmp(str,token1))==0)
            {
              printf("in strcmp\n");
              fflush(stdout);
              printf("There are %d of tuples in this relation. \n", schLine);//print
              right =1;
            }
    I made few changes so it looks more clearly

    output:
    unix2% p3 config_file.txt query_file.txt
    calling count
    curline in count = count Students

    token1 count
    token1 Students
    end of token
    size = 2
    str= Students
    Segmentation Fault (core dumped)

    thanks guys I found the error haha finally beat it!!!
    the error is token == NULL after strtok is over and you cant print or compare it lol what a stupid mistake
    Last edited by p595285902; 03-23-2011 at 03:34 PM.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Raise your hand if you understand why global variables make things difficult for you:
    Code:
    void count(void)//the founction print out the number of relations
    {
      char * token1;
      //  char * attname;
      char * str;
      int size;
      int x =0;
      int right =0;
      printf("curline in count  = %s\n", curline);
      token1 = strtok(curline, " \r\t\n");//use strtok to get the first word
      while(token1 != NULL)
        {
          printf("token1 %s\n",token1);
          token1 = strtok (NULL, " \r\t\n");//continue to part the string
        }
      printf("end of token\n");
      size =tableSize;//set size
      printf("size = %d\n",size);
      while(size>0 && right == 0)//if size is>0 and right is 0
        {
          str = relation[x].name;
    This is what now? And we know it's good because ... ?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    str can be printed and it works fine I think you told me to use relation[x].nemae yesterday thx man

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by p595285902 View Post
    str can be printed and it works fine I think you told me to use relation[x].nemae yesterday thx man
    That has nothing to do with what I said. I said it's global, and we who are just looking at that function you provided us, have no idea what it is, or what value you may or may not have entered into it. I know what the syntax means. That doesn't mean I know what value it happens to have in it at runtime.

    Why aren't you actually checking to see if tokens is null or not, instead of just trying to print it? Most sane implementations of printf just print (null) when they encounter one, but as far as I know it's not a requirement, so crashing is perfectly acceptable.


    Quzah.
    Last edited by quzah; 03-23-2011 at 03:57 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM