Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » printf and scanf behave poorly in Eclipse 3.5 on Windows
printf and scanf behave poorly in Eclipse 3.5 on Windows [message #533702] Sat, 15 May 2010 19:17 Go to next message
patfla  is currently offline patfla Friend
Messages: 2
Registered: May 2010
Junior Member
I think I've seen messages to this effect already.

Very simple program testing out CDT:

/*
 * Top.c
 *
 *  Created on: May 11, 2010
 */

/* scanf demo */
#include <stdio.h>

int main()
{
    char str[80];

    printf("Enter your surname: ");
//    fflush(stdout);
    scanf("%s",str);
    printf("Hello Mr. %s\n",str);

    return(0);

}


When I run in the debugger (gdb) with the fflush commented out (as above) I get:

Quote:
Smith
Enter your surname: Hello Mr. Smith



I took me a while before I realized that the blank console window was waiting for my scanf() input without the program's having output the previous printf() prompt.

Yes I did try putting a newline (\n) at the end of the first printf - didn't help.

I comment the fflush back in and get:

Quote:
Flaherty
Enter your surname: *stopped,reason="breakpoint-hit",disp="keep",bkptno="2",f ...



Try to step past this; continue over it; enter various things into the console window and hit return. Nothing helps.

In principle the fflush should flush the stdout IO from the first printf() and all should be fine. Apparently not.

What's most interesting however is when I then take the same code and move it to Linux. OpenSUSE 11.2 running inside VirtualBox on my Windows 7 machine. It seems that latest version of Eclipse that OpenSUSE will install from its repositories is Ganymede (what's that 3.4?). No mind, continue on.

This same code, with its printf()s and scanf() behaves exactly as I want and would expect under Linux. That is, none of the unexpected problems above when I ran on Windows.

It seems that CDT, stdout and stdin in Eclipse and the Eclipse console work less well on Windows than Linux?

Is there anything I can to fix the Windows version of Eclipse? I don't mind trying to hack the Eclipse code and perhaps learn a few things.
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #533732 is a reply to message #533702] Sun, 16 May 2010 11:50 Go to previous messageGo to next message
Axel Mueller is currently offline Axel MuellerFriend
Messages: 1973
Registered: July 2009
Senior Member
The Eclipse console has buffering problems on Windows. Basically, it doesn't flush the streams when a newline is received. (Unlike a normal windows console window)
Either, you have to add fflush calls when needed or add the following lines in the start of the main function:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);


Before you ask
- search this forum
- see the FAQ http://wiki.eclipse.org/CDT/User/FAQ
- google
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #547385 is a reply to message #533732] Fri, 16 July 2010 14:25 Go to previous messageGo to next message
linziza is currently offline linzizaFriend
Messages: 5
Registered: July 2010
Junior Member
I'm having the same problem in Eclipse Helios. I've tried adding
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
at the top of main.

As well as adding
fflush(stdout);
after every instance of printf.

STILL not working properly. I save my file after editing, then build before running.

Any other ideas for a workaround?

Thanks,
Linziza
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #547469 is a reply to message #547385] Sat, 17 July 2010 09:49 Go to previous messageGo to next message
Edzard Egberts is currently offline Edzard EgbertsFriend
Messages: 57
Registered: July 2009
Member
linziza schrieb:
> I'm having the same problem in Eclipse Helios. I've tried adding
> setvbuf(stdout, NULL, _IONBF, 0);
> setvbuf(stderr, NULL, _IONBF, 0); at the top of main.
>
> As well as adding fflush(stdout);
> after every instance of printf.
>
> STILL not working properly. I save my file after editing, then build
> before running.
>
> Any other ideas for a workaround?

Yes, you can open a console for your own and workaround Eclipse console.
With the following code (header and object file) you just need to define
a "konsole MyOutput;" at beginning of your program, or use the very
simple code directly ("konsole" instead of "console" is not wrong, but
german - I like to use an enlarged namespace ;o).

========================================================
Konsole.h:
--------------------------------------------------------
#ifndef KONSOLE_H_
#define KONSOLE_H_

class Konsole
{ // Konsole für Standardio initialisieren und beenden.
// Klasse als Variable im Hauptprogramm anlegen
public:
Konsole();
virtual ~Konsole();
};

#endif /*KONSOLE_H_*/

========================================================
Konsole.cpp:
--------------------------------------------------------
#include "Konsole.h"

#ifdef WIN32
#include <iostream>
#include <windows.h>

Konsole::Konsole()
{
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
}

Konsole::~Konsole()
{
fclose(stdin);
fclose(stdout);
fclose(stderr);
FreeConsole();
}
#endif
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #558403 is a reply to message #547469] Sun, 12 September 2010 12:32 Go to previous messageGo to next message
jws01 is currently offline jws01Friend
Messages: 3
Registered: September 2010
Junior Member
I tried all versions (setvbuf, fflush, Konsole).
Nothing helps.

I didn't get a separate console outside of eclipse.
I'am using WinXP and wascana.

How I get an extra console?

Thanks,
jws01
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #1810114 is a reply to message #533732] Wed, 31 July 2019 15:56 Go to previous messageGo to next message
Jugal Gala is currently offline Jugal GalaFriend
Messages: 1
Registered: July 2019
Junior Member
Axel Mueller wrote on Sun, 16 May 2010 11:50
The Eclipse console has buffering problems on Windows. Basically, it doesn't flush the streams when a newline is received. (Unlike a normal windows console window)
Either, you have to add fflush calls when needed or add the following lines in the start of the main function:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);


I was having the same issue and this works perfectly well for me on Eclipse 4.12 with Cygwin and Windows 10 LTSB 2016. Thank you!
Re: printf and scanf behave poorly in Eclipse 3.5 on Windows [message #1810670 is a reply to message #533702] Thu, 15 August 2019 16:46 Go to previous message
Gordon Doughman is currently offline Gordon DoughmanFriend
Messages: 10
Registered: December 2018
Junior Member
I ran into this problem while teaching a C class using Eclipse Proton.

To have an external console created during debugging, go to "Debug Configurations..." and select the debug configuration associated with the application. Next, select the "Debugger" tab and then check the "Use external console for inferior (open a new console window for input/output)" check box.

Don't try to single step through scanf() statements; It doesn't work. Set a breakpoint just after the scanf() statement and use "Resume" to execute the scanf() statement.

Hope this helps.

Previous Topic:Eclipse 2019-06 looks for glibc source code
Next Topic:error during run in gnu mcu eclipse
Goto Forum:
  


Current Time: Fri Apr 19 12:02:35 GMT 2024

Powered by FUDForum. Page generated in 0.02070 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top