Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c++ - Eclipse debugging with input from console

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.
", 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
");
            break;
        }
    };
    fclose(inputFile);
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...