8

I'm trying to debug a program I wrote in C++ using Eclipse. The program includes getting input from the user but when I enter the input to the console it won't ever continue running the code (it'll keep asking for input). I can't debug without fixing this and would appreciate some help. Thank you.

The code gets stuck on the while loop fgets:

int main(int argc, const char**argv) {
    FILE* inputFile = NULL;

    setlocale(LC_ALL, "");
    if(argc == 2){
        inputFile = fopen(argv[1], "r");
        if (inputFile == NULL){
            printf("Problem opening file %s, make sure correct path name is given.\n", argv[1]);
            return 0;
        }
    }
    else {
        inputFile = stdin;
    }

    char buffer[MAX_STRING_INPUT_SIZE];
    // Reading commands
    while ( fgets(buffer, MAX_STRING_INPUT_SIZE, inputFile) != NULL ) {
        fflush(stdout);
        if ( parser(buffer) == error ){
            printf("ERROR\n");
            break;
        }
    };
    fclose(inputFile);
    return 0;
}
6
  • 2
    Can you attach relevant code here?
    – hmatar
    Jan 14, 2013 at 18:40
  • 1
    Do you get the same problem running the program outside of Eclipse as a stand alone executable?
    – user645280
    Jan 14, 2013 at 18:42
  • See if this helps: stackoverflow.com/questions/4711098/…
    – queen3
    Jan 14, 2013 at 18:44
  • Sounds like an infinite loop. Jan 14, 2013 at 18:48
  • It's not. When I run it in the terminal it works.
    – matanc1
    Jan 14, 2013 at 18:52

2 Answers 2

14

The issue comes from Eclipse buffering the console inputs. One way to fix it is to force Eclipse to debug using a Windows/DOS native console.

The procedure is described in details here, but in brief :

  1. Create your Hello World C++ command line project, from the Eclipse menu File > New > C++ Project
  2. In your project folder, create a ".gdbinit" text file. It will contain your gdb debugger configuration
  3. Edit ".gdbinit", and add the following line (without quotes) : "set new-console on"
  4. In Eclipse, go to menu Run > "Debug Configurations", and select your application name in the left pane
  5. In the "debugger" tab, ensure the "GDB command file" now points to your « .gdbinit » file. Else, input the path to your ".gdbinit" configuration file
  6. Click « Apply » and « Debug ». You’re done ! A native DOS command line should be launched.
1
  • how to stop Eclipse buffering the console inputs while 'Running the Application As'(not dedugging) too? Apr 15, 2017 at 15:18
0

Well I know that Eclipse console buffer doesn't always work as it should - in my experiences, it's the worst when using C or C++.

If you want to run your compiled code in Eclipse, this will greatly help but not eliminate the display issues you might encounter in Eclipse:

setvbuf(stdout, NULL, 0, _IONBF);

However, I don't know how this affects debugging in Eclipse while using stdin. Your best option is to use Nicolas's answer to run through the DOS console. A bit more work, but it will suffice I believe.

And please note that the above line should only be used while running your code through Eclipse, in order to have some kind of sane output buffer without several messy in-code workarounds. This line needs to be commented out when compiling for actual use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.