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 |
patfla 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 #547469 is a reply to message #547385] |
Sat, 17 July 2010 09:49 |
Edzard Egberts 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 #1810114 is a reply to message #533732] |
Wed, 31 July 2019 15:56 |
Jugal Gala Messages: 1 Registered: July 2019 |
Junior Member |
|
|
Axel Mueller wrote on Sun, 16 May 2010 11:50The 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!
|
|
| |
Goto Forum:
Current Time: Fri Apr 19 12:02:35 GMT 2024
Powered by FUDForum. Page generated in 0.02070 seconds
|