Thread: Need some help debugging

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    Need some help debugging

    I am trying to debug a program for segmentation fault, and did research online. The content I found did not seem to cover what I see, so I like to ask for help. Here is the output:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x6f337044 in ?? ()
    (gdb) backtrace
    #0  0x6f337044 in ?? ()
    #1  0x64713357 in ?? ()
    #2  0x43756950 in ?? ()
    #3  0x414a5377 in ?? ()
    #4  0x6f6d5141 in ?? ()
    #5  0x5a724453 in ?? ()
    #6  0x30366330 in ?? ()
    #7  0x2d6a747a in ?? ()
    #8  0x52594e74 in ?? ()
    #9  0x62685343 in ?? ()
    #10 0x6e436e6d in ?? ()
    #11 0x62455143 in ?? ()
    #12 0x664b6f77 in ?? ()
    #13 0x4f6a7134 in ?? ()
    #14 0x485f5832 in ?? ()
    #15 0x42386c31 in ?? ()
    #16 0x674e436f in ?? ()
    #17 0x775f7758 in ?? ()
    #18 0x000a4263 in ?? ()
    #19 0x0025c000 in ?? ()
    Cannot access memory at address 0x5242354b
    (gdb)
    Here is the code:

    Code:
    1 #include <stdio.h>
          2 int main ()
          3 {
          4   FILE *fp;
          5   printf ("Enter any character please ");
          6   getch();
          7   fp = fopen ("somefile.txt", "r");
          8   if (fp == NULL)
          9   {
         10     puts("Could not open the file");
         11     exit(1);
         12   }
         13   int i, buffer;
         14   char ch, line [200];
         15   printf ("Press 'C/c' to continue or 'q/Q' to quite");
         16   while ((ch=getch())=='c'|| ch == 'C')
         17   {
         18     for ( i=0; i<19; i++)
         19     {
         20       fgets(line, buffer, fp);
         21       printf("%s", line);
         22     }
         23     if (ch == 'q' || ch == 'Q')
         24       break;
         25     if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
         26       puts ("You did not provide a valid response, try again");
         27   }
         28 }
    I think line 26 may mess things up. I tried to set a watch for 'ch' since for some reason on line 26 it has value of & in it! not sure how. I thought if I put a watch, then gdb would let me know when the value changes. But I did not get anything:-(

    Here is the session output...

    Code:
    Breakpoint 1, main () at e6.c:23
    23          if (ch == 'q' || ch == 'Q')
    (gdb) n
    25          if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
    (gdb) p ch
    $1 = 38 '&'
    (gdb) help

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Are you building your code with debugging enabled (-g)?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by Elkvis View Post
    Are you building your code with debugging enabled (-g)?
    Yea; -g option.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The crash problem is probably line 20. You probably should re-read your documentation for fgets() and be sure you provide the correct values for the parameters. Look carefully at buffer.

    However your if statement is probably logically wrong as well. Watch out for all those negations when using the OR operator.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Also, I don't see you including any headers that declare getch(). getch() is a nonstandard function, and should generally be avoided.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You also haven't #included the correct header to use exit().

    If you properly post your code (without the line numbers) you might get better advice in the future.


    Jim

  7. #7
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by jimblumberg View Post
    The crash problem is probably line 20. You probably should re-read your documentation for fgets() and be sure you provide the correct values for the parameters. Look carefully at buffer.

    However your if statement is probably logically wrong as well. Watch out for all those negations when using the OR operator.

    Jim
    Thanks. I initialized the buffer to 200, and now it runs better:-) I need to fix one more thing.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I initialized the buffer to 200, and now it runs better
    You should probably be using a named constant for the size of this array.
    Code:
    #define LINE_SIZE 200
    
    ...
    
    char ch;
    char line [LINE_SIZE];
    ...
    fgets(line, LINE_SIZE, fp);
    Jim

  9. #9
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Updated working code:

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
      FILE *fp;
      printf ("Enter any character please ");
      getch();
      fp = fopen ("somefile.txt", "r");
      if (fp == NULL)
      {
        puts("Could not open the file");
        exit(0);
      }
      int i, ch, buffer;
      ch = 0;
      buffer = 200;
      char line [200];
      printf ("Press 'C/c' to continue or 'q/Q' to quite");
      ch = getch();
      while ((ch=='c') || (ch == 'C'))
      {
        for ( i=0; i<19; i++)
        {
      buffer = 200;
          fgets(line, buffer, fp);
          //printf("%s", line);
          puts(line);
        }
        printf ("Press 'C/c' to continue or 'q/Q' to quite");
        ch = getch();
        if (ch == 'q' || ch == 'Q')
          break;
        if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
        puts ("You did not provide a valid response, try again");
      }
    }
    This should have all the recommendations given. Is there a better way to handle the multiple or situations? I do not know much about implementing flags; I believe it is what I need. I did a search online to see how to create two flags for continue and stop, but the example was a lot more elaborate than what I need now and I could follow it. I guess I can use if statement. Something like ...

    Code:
    if ( ch == 'q' || ch == 'Q') continue = 0;
    if ( ch == 'c' || ch == 'C') continue = 1;
    Would this be the best way to implement what I am trying to do or is there cleaner, more simple method?
    Last edited by zolfaghar; 06-06-2016 at 10:42 AM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by zolfaghar View Post
    This should have all the recommendations given.
    Except the named constant for the buffer size, and the fact that getch() is nonstandard, and you're not including a header for it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by Elkvis View Post
    Except the named constant for the buffer size, and the fact that getch() is nonstandard, and you're not including a header for it.
    I posted the old code from my clipboard.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by zolfaghar View Post
    Would this be the best way to implement what I am trying to do or is there cleaner, more simple method?
    You can simplify it a lot. I would press Q/q to quit, and any other key to continue. The extra conditions are redundant. If you respond with anything other than [qQcC], you continue anyway, so why not just eliminate the C/c option?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You really don't need either of those if() statements at the bottom of your loop because you're while() statement limits the loop to only accepting 'C' or 'c'.

    Jim

  14. #14
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    I am now trying to print specific columns of each line according to the user's input and I just can not make it work right. I have tried to user getchar() and scanf() to get the user's input. At times the program just skips over those statements. At other times it does what I want but it outputs a whole bunch of printf () statements and I can not figure out why it does that. I think the skipping of scanf() may be due to an issue of scanf() grabbing what is left in the buffer. I am not sure. Here is my code; would you have a look?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
      FILE *fp;
      fp = fopen ("somefile.txt", "r");
      if (fp == NULL)
      {
        puts("Could not open the file");
        exit(0);
      }
      int i, ch, buffer, m, n;
      ch = 0;
      buffer = 200;
      char line [200];
      printf ("Press 'C/c' to continue or 'q/Q' to quite");
      while ((ch=getchar()=='c') || (ch == 'C'))
      {
        for ( i=0; i<19; i++)
        {
      buffer = 200;
          fgets(line, buffer, fp);
          //printf("%s", line);
          //puts(line);
          printf (" \n Enter the value of starting column ");
          scanf("%d", &m);
          printf (" \n Enter the value of ending column ");
          scanf ("%d", &n);
          int columCounter;
          for (columCounter = m; columCounter < n+1; columCounter++)
          putchar(line[columCounter]);
          printf ("Press 'C/c' to continue or 'q/Q' to quite");
          ch = getchar();
          //scanf ("%d", &ch);
          if (ch == 'q' || ch == 'Q')
            break;
          if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
          puts ("You did not provide a valid response, try again");
        }
      }
    }

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think the skipping of scanf() may be due to an issue of scanf() grabbing what is left in the buffer.
    Close. The problem may be that there is a \n left in the buffer for getchar() to grab, where you ask about continuing or quitting. Simply call getchar after scanf() to clean up after it.

    Code:
    for ( i=0; i<19; i++)
    Does this loop have a purpose?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging help :(
    By Zul56 in forum C Programming
    Replies: 51
    Last Post: 01-12-2013, 10:21 PM
  2. Need help debugging...
    By n2d33p88 in forum C++ Programming
    Replies: 8
    Last Post: 12-13-2007, 04:15 AM
  3. Debugging help
    By iitb.ankit in forum C Programming
    Replies: 4
    Last Post: 06-04-2006, 07:20 AM
  4. help debugging
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2005, 01:48 PM
  5. debugging
    By Ssfccsh in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2004, 12:20 AM

Tags for this Thread