Like Tree1Likes

strcmp error

This is a discussion on strcmp error within the C Programming forums, part of the General Programming Boards category; Code: while(fgets(line,Char,que)!=NULL && strcmp(line,"quit") !=0)//when the command is not eof { printf("line == %s\n", line); token1=strtok(line," \n\t");//get the command while(token1 ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    53

    strcmp error

    Code:
    while(fgets(line,Char,que)!=NULL && strcmp(line,"quit") !=0)//when the command is not eof
        {
          printf("line  == %s\n", line);
          token1=strtok(line," \n\t");//get the command
          while(token1 != NULL)//when command is not quit
            {
              printf("token1 = %s\n",token1);
              if(strcmp("count",token1) ==0)//when command is count
                {
    this is part of my code
    the line
    if(strcmp("count",token1) ==0)//when command is count
    gave me error Segmentation Fault (core dumped)

    whats wrong with it?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    I don't see anything immediately wrong with your code. How are Char and token1 declared? What is the output of the printf immediately above your strcmp? Give us enough code and sample input to reproduce the error.

    Have you learned how to use a debugger yet? It's one of the most useful and powerful tools for a programmer, and allows you to examine and change the value of each variable as you go through your program line by line, so you would be able to track this stuff down yourself. This page is a great place to start: Cprogramming.com - Debuggers.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    output:
    token1 = count


    char * token1;
    #define Char 100;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    For the love of all things good and holy, don't #define Char as 100. It's easily confused for char, which is a reserved word, and it's very non-descriptive. Try something like LINE_LEN or MAX_LINE. Also, assuming line is a char array declared locally is to do something like
    Code:
    fgets(line, sizeof(line), que)
    That way, if you change line from "char line[Char]" to "char line[SOME_OTHER_CONST]", you don't need to worry about changing your fgets call.

    I will need enough of your program, and perhaps the contents of an input file to reproduce the error. I filled in the gaps around what you provided and couldn't get the program to seg fault. Here's what I have:

    foo.c:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LINE_LEN 100
    
    int main(void)
    {
        char line[LINE_LEN],
             *token1;
        FILE *que;
    
        que = fopen("foo.txt", "r");
        if (que == NULL) {
            perror("fopen");
            return 1;
        }
    
        while(fgets(line,sizeof(line),que)!=NULL && strcmp(line,"quit") !=0)//when the command is not eof
        {
            printf("line  == %s\n", line);
            token1=strtok(line," \n\t");//get the command
            while(token1 != NULL)//when command is not quit
            {
                printf("token1 = %s\n",token1);
                if(strcmp("count",token1) ==0)//when command is count
                {
                    printf("Equal!\n");
                }
                token1 = strtok(NULL, " \n\t");
            }
        }
    
        fclose(que);
    
        return 0;
    }
    foo.txt:
    Code:
    This is a test
    Of the emergency broadcasting system
    quit
    Like I said, there's nothing wrong with what you posted, so either you didn't post the actual code, or there's something somewhere else that's screwing it up. I need the smallest, compilable piece of code and sample input that demonstrates the error.
    Last edited by anduril462; 03-23-2011 at 01:14 PM. Reason: Fixed code

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Program received signal SIGSEGV, Segmentation fault.
    0xff22f6c8 in memccpy ()

    this is error fomr gdb, does this line tells me which and and which file?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    A full backtrace would be more helpful to you. It will give you file and function info and line numbers. You probably also want to compile with debugging symbols on. Try "gcc -Wall -g file.c". Then, run the program until it segfaults. When that happens, type "backtrace" and you'll get the full stack all the way back to where it called main.

    Also, notice how it reports a problem in memccpy? That's a copying function, not a comparison function. Are you sure that's exactly where your error is? I don't see any strcpy/memcpy stuff going on in what you posted.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    I dont know what I did but I moved my problem to the above line,
    Code:
    ^D
    ^X^D
    pe
    p3
    run
    file "p3
    quit
    how do I get out from dgb...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    You "moved your problem"? Does that mean it's now the printf? Did you move it, or did it just manifest there this time? Seg faults, since they are caused by accessing invalid memory, result in undefined behavior. That means they often show up in different places each time you run your program. For the last bloody time, give me the smallest piece of code that compiles and produces the problem. If you don't listen to me, why should I listen to you?

    I assume that's your input file there. That input worked with my program. You should read the fgets documentation, particularly about how it handles new lines it read. Then look at the strcmp in your loop.
    stahta01 likes this.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Quote Originally Posted by p595285902 View Post
    I dont know what I did but I moved my problem to the above line,
    Code:
    ^D
    ^X^D
    pe
    p3
    run
    file "p3
    quit
    how do I get out from dgb...
    those are my gdb screen, try to get out of gdb

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    Open up a new terminal and type "man gdb". You can also type "help" inside GDB for the help menu.

  11. #11
    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);
          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;
      token1 = strtok(curline, " \t\n");//use strtok to get the first word
      printf("token1 = %s\n", token1);
      while(token1 != NULL)
        {
          token1 = strtok (NULL, " \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;
            }
    .
    .
    .
    files compiled and the
    output:
    calling count
    token1 = count
    end of token
    size = 2
    str= Students
    Segmentation Fault (core dumped)


    all those codes are part of the file. I would like to show more if it is needed.
    char curline[100];
    Last edited by p595285902; 03-23-2011 at 02:38 PM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On a guess... strcmp("count",token1) == 0 ... is failing because token1 has a trailing newline or carriage return on the end of it. Windows line ends are not just a newline (0x0a) it's actually a combination of cr/lf (0x0d and 0x0a) if you're not cleaning line ends, the cr or lf may be left causing a mismatch. Changing your strtok() qualifier to "\r\n\t " might just clear it up.
    Last edited by CommonTater; 03-23-2011 at 02:53 PM.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    Code:
      while(token1 != NULL)
        {
          token1 = strtok (NULL, " \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)
            {
    Look at that loop. What condition makes it stop? Now look at that printf. Why do you think it's seg faulting? What are you actually trying to do with that loop?

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    it is probably token1's fault. But everything just looks normal to me

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    It is token1's fault. That loop I highlighted only stops when token1 is NULL. Then you try to print the string at the address in token1, which is NULL. Dereferencing NULL will usually cause a seg fault. Now, what are you trying to do in that loop and what do you expect to be in token1 when it's done?

Page 1 of 2 12 LastLast
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, 03: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, 04:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21