Like Tree9Likes

The mystical art of debugging

This is a discussion on The mystical art of debugging within the C Programming forums, part of the General Programming Boards category; Hi all. I am learning how to use the debugger. Here is a program that I wrote in the early ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    The mystical art of debugging

    Hi all. I am learning how to use the debugger.

    Here is a program that I wrote in the early days of learning C for displaying the contents of a text file to the screen.

    I had always thought that it would grab 100 characters of a line(at a time) and then output to the screen, looping repeatedly, until fgets() == NULL. To my surprise, it seems to be storing several lines somewhere, and then only outputting them when fgets() == NULL. So it seems to output only once. Why is it doing this, and how can it be doing this when the buffer size is only 100?

    Thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_FILE_LENGTH 40
    #define MAX_STRING_LENGTH 100
    
    int main(void)
    {
        int count, file_length, string_length;
        char filename[MAX_FILE_LENGTH], line[MAX_STRING_LENGTH];
        FILE *openfile;
    
        puts("Enter file to open");
        fgets(filename, MAX_FILE_LENGTH, stdin);
    
        string_length = strlen(filename);
    
        filename[string_length-1] = '\0';
    
        puts(filename);
    
        if ((openfile = fopen(filename, "r")) != NULL)
            puts("File opened successfully\n");
    
        else
            {
                puts("Error opening file");
                exit(1);
            }
    
        while(fgets(line, MAX_STRING_LENGTH, openfile ) != NULL)
            {
                printf("%s", line);
            }
    
        fclose(openfile);
    
        return 0;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Put a breakpoint on the while and watch line, then step for some time, what is happening? That should prove things to you.

    I think your program works correctly, the results are just what you end up experiencing. The screen is buffered so you will probably see the entire file at once, or huge blobs of text appear at once (if you actually bump against the console's buffer size) rather than a line by line progression like a video game. All those delays are artificially made.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    What has the title "mystical art of debugging" got to do with your problem?

    In any event, you need to read up more carefully on how fgets() works, in particular when a '\n' (and a terminating '\0') is included in the buffer, and when it is not. Compare that with what your screen does with long lines (a lot of screens simply discard the data on very long lines).
    cfanatic likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by whiteflags View Post
    Put a breakpoint on the while and watch line, then step for some time, what is happening? That should prove things to you.
    line does store 100 characters at a time(as expected), but the 100 characters changes constantly when I step through the loop, and there is no output while this happens. And then everything is dumped to the screen. Where are all the multi x 100 characters stored? It's dumping about 2000 characters, so they must all be stored somewhere? Does fgets() put it all on the stack?
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by grumpy View Post
    What has the title "mystical art of debugging" got to do with your problem?
    He was strongly guided in another post to learn how to use a debugger,so he might be influenced by that

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by grumpy View Post
    how fgets() works
    cfanatic take a look in here fgets - C++ Reference
    Code:
    Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
    
    A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
    cfanatic likes this.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Quote Originally Posted by cfanatic View Post
    Does fgets() put it all on the stack?
    No. stdout (which is used by printf()) is buffered, and one of the conditions for clearing the buffer is '\n' characters in output. You may also, if your file contains long lines, see things missing due to how your output device (screen) handles long lines.

    Quote Originally Posted by std10093 View Post
    He was strongly guided in another post to learn how to use a debugger,so he might be influenced by that
    That doesn't justify using subject lines for threads that are unrelated to content of posts.
    cfanatic likes this.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by cfanatic View Post
    line does store 100 characters at a time(as expected), but the 100 characters changes constantly when I step through the loop, and there is no output while this happens. And then everything is dumped to the screen. Where are all the multi x 100 characters stored? It's dumping about 2000 characters, so they must all be stored somewhere? Does fgets() put it all on the stack?
    Well, it's being stored in a low level buffer somewhere, and the output is flushed when the buffer is full, most likely.

    If you have to see something, force the characters to be written to the screen with fflush(stdout);
    cfanatic likes this.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by grumpy View Post
    That doesn't justify using subject lines for threads that are unrelated to content of posts.
    True,so cfanatic will be more careful next time :-D

    Also cfanatic my roomate is learning C now and they had as topic today the gnome debugger.I will copy paste a list of commands you use in the terminal in order to play around with gdb
    Code:
    debugger (gdb) commands
    Most frequently used gdb commands (ask the man page)
    
    run | Start your program.
    quit | Exit from gdb.
    help | Show information about command names and classes (e.g. help
    breakpoints).
    print | Display the value of an expression.
    display | Print value of an expression each time the program stops.
    continue | Running your program (after stopping).
    next | Execute next program line (after stopping); step over.
    step | Execute next program line (after stopping); step into.
    return | Execute until caller (after stopping); step back.
    break | Set a breakpoint at function (in file).
    list  | type the text of the program in the vicinity of where it is presently
    stopped.
    backtrace | display the program stack.
    up | Select and print stack frame that called this one.
    down | Select and print stack frame called by this one
    in an IDE of course you have them visualized with elegant figures but i thought it might be helpful to know some things about it
    cfanatic likes this.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by whiteflags View Post
    Well, it's being stored in a low level buffer somewhere, and the output is flushed when the buffer is full, most likely.
    Yep, this appers to be the case. I have a large paragraph, and it is outputting chunks of the paragraph at a time, so I guess the buffer was full and getting flushed.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  11. #11
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    True,so cfanatic will be more careful next time :-D

    Also cfanatic my roomate is learning C now and they had as topic today the gnome debugger.I will copy paste a list of commands you use in the terminal in order to play around with gdb
    Code:
    debugger (gdb) commands
    Most frequently used gdb commands (ask the man page)
    
    run | Start your program.
    quit | Exit from gdb.
    help | Show information about command names and classes (e.g. help
    breakpoints).
    print | Display the value of an expression.
    display | Print value of an expression each time the program stops.
    continue | Running your program (after stopping).
    next | Execute next program line (after stopping); step over.
    step | Execute next program line (after stopping); step into.
    return | Execute until caller (after stopping); step back.
    break | Set a breakpoint at function (in file).
    list  | type the text of the program in the vicinity of where it is presently
    stopped.
    backtrace | display the program stack.
    up | Select and print stack frame that called this one.
    down | Select and print stack frame called by this one
    in an IDE of course you have them visualized with elegant figures but i thought it might be helpful to know some things about it
    Thanks for the info. I am debugging in Code::Blocks(using gdb). I'll be using IDE debugging for the rest of my life.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by cfanatic View Post
    Thanks for the info. I am debugging in Code::Blocks(using gdb). I'll be using IDE debugging for the rest of my life.
    Your welcome.Glad to hear that,because this means that you like it Maybe you can teach me how to use it in the future :-D

  13. #13
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    Your welcome.Glad to hear that,because this means that you like it Maybe you can teach me how to use it in the future :-D
    Right now I only know how to watch the values of local variables. Still have to to learn how to set breakpoints and and whatever else is involved with debugging.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    There are a couple of ways that I know.

    Click between the number lines and the code to set a breakpoint.
    Or you can set the text cursor amid a line and access Debug -> Toggle breakpoint.
    Or you can set the text cursor amid a line and press F5.

    Actually the hardest way to set a breakpoint in the world may be with a poorly suited editor and a command line interface. It can be hard to set breakpoints that way when there is basically no line numbering. I guess most IDEs make it easy.
    cfanatic likes this.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by grumpy View Post
    That doesn't justify using subject lines for threads that are unrelated to content of posts.
    With all due respect (since grumpy is one of my favorite posters) I like the subject line. It may be a little overly-creative, but at least it says something about the contents. What I hate are all the "help me" subjects. They show no thought at all and are utterly useless.

    On using debuggers, it seems to me that they aren't introduced to students early enough (or at all). They are a great way to deepen your understanding and should be introduced as early as possible.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C, need debugging help
    By Theta Zero in forum C Programming
    Replies: 15
    Last Post: 02-16-2011, 09:49 PM
  2. debugging
    By bean66 in forum C Programming
    Replies: 7
    Last Post: 08-21-2009, 04:33 PM
  3. debugging
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 09-16-2007, 07:22 PM
  4. Debugging?
    By Brain Cell in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 12:00 AM
  5. need some debugging help
    By InvariantLoop in forum C Programming
    Replies: 15
    Last Post: 02-09-2005, 08:53 PM

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