Console Application - console

Edit:
I don't know what this user originally wanted, and hopefully they'll edit their question to let us know, but otherwise, let's use this question to answer (or give links to) the following common console window issues:
How do you capture the output of a console application in your program (for instance, running a build process and getting the output in your IDE)?
How do you get your console application to hang around long enough to see the output when you hit "run" in the IDE? (ie, getch for C, some IDEs have options to set, what common/popular pause and wait for keypress routines do you use to keep the console window open long enough to see the output? This applies to lots of languages - list your method)
Original question:
How to view console application output
screen(black screen).Please mention in
detail.

Keeping the console window open in for C++ in a standard's compliant way (not platform specific):
#include <iostream>
#include <limits>
int main() {
// Rest of the code
//Clean the stream and ask for input
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();
return 0;
}
Source.
-Adam

Keeping the console window open in C:
/* Example waits for a character input */
#include <stdio.h>
int main()
{
/* Put your code here */
getchar();
return 0;
}
getchar is standards compliant, while getch (a common usage) is platform specific.
-Adam

In c# I just put a break point at the end of my code to keep the console window open. I used to use Console.Read(); but got sick of typing it...
Edit: btw I just use this for my debugging purposes. If it needs to be a feature then Console.Read();

If it's a graphical application that happens to write messages to standard output, open your favorite terminal application and type in the command there. If its output exceeds the output buffer of your terminal, pipe it through 'more' or 'less'.

Related

No Way to get the console handle (Dev-Pascal)

I've tried the simple program for my exercise but if i compiled, it turned give a message "No Way to get the console handle" (Dev-Pascal).
Here's my code:
program square;
uses crt;
var
side,circumference,broad:real;
begin
write('Input the side value of the square: ');
readln(side);
circumference := 4 * side;
broad := side * side;
writeln('The circumference value of the square = ', circumference);
writeln('The abroad value of the square = ', broad);
writeln();
write('Press any key...');
readkey();
end.
thank you for helping and teaching me
i would appreciate
Dev-pascal comes with a 10+ years old Free Pascal version. I really have no idea. Try to run the example on the cmdline to see if dev-pascal is the cause or something else. Temporarily disable security software might also be something to try.
I suppose, you've created a Window Skeleton project.
It has it's description: A Windows skeleton application (no window).
In this case you'll get this error though you code compiles (I've got this error too with your code).
Try to create Console Application project which has it's own description: A standard console-based (MS-DOS) application (Your code has compiled and the program worked as well).
P.S. Can't say what exactly happens -- couldn't find any documentation about errors in Dev-Pas (1.9.2).
My assumption is that skeleton program doesn't imply console window for execution program while console application does.
just close the project and then re-open the pascal source file (.pas). And maybe put "readln;" just before "END.".

Autohotkey wrong keystrokes sent to console

I am trying to understand this bug and I am looking for a workaroud.
Using this script:
#NoEnv
#SingleInstance force
SendMode Input
;Alt+t to send keystrokes
!t::Send, /[]
It send the correct keystrokes /[] to all windows but the windows console (cmd)
Additional Info:
Using autohotkey v1.1.09.02
With an english US keyboard layout, it sends: '90
With a french canadian multilingual keyboard layout, it sends: é^ç
Any idea of what can fix it?
Try this:
#NoEnv
#SingleInstance force
;SendMode Input
;Alt+t to send keystrokes
!t::Send, % chr(047) chr(091) chr(093)
Return
And let me know if it solves your issues.
Found this:
I use multiple languages or keyboard layouts on my system. Why do Send and Hotstrings sometimes send the wrong characters?
This can happen whenever the script's language or keyboard layout does not match that of the active window. To fix it, open the script's main window via its tray icon. While the main window is active, use the language bar (or a language hotkey such as LeftAlt+Shift) to change the script's language/layout to match that of the window you are currently typing in. Switching the script's language can be automated with the following example hotkey:
#l:: ; Win+L hotkey.
ListLines ; Show the script's main window.
WinWaitActive ahk_class AutoHotkey
Send {LAlt down}{Shift}{LAlt up} ; Switch to alternate language (keys must be in this format).
WinMinimize ; Minimize the window found by WinWaitActive above.
return
More info: Like all applications, each script starts off using your default language. If the default does not match that of the active window (where the keystrokes are sent), the difference in keyboard layouts might cause keystrokes sent by the script to be translated into something unexpected.
On: http://autohotkey.free.fr/docs/FAQ.htm#load

Strange error with QListWidgetItem that uses QIcon

I am building up a QListWidget, browsing through a directory so that every ".png" gets listed with a preview icon.
The core of my populating loop looks like this:
new QListWidgetItem( QIcon(act_fullname), act_filename);
Right after the whole list is ready, the app crashes.
The error is many times repeated and says this:
On Mac OS X, you might be loading two sets of Qt binaries into the
same process. Check that all plugins are compiled against the right Qt
binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of
binaries are being loaded. QObject::moveToThread: Current thread
(0x103339cb0) is not the object's thread (0x10a848670). Cannot move to
target thread (0x103339cb0)
On Mac OS X, you might be loading two sets of Qt binaries into the
same process. Check that all plugins are compiled against the right Qt
binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of
binaries are being loaded.
Do you have any ideas?
Thanks for your help!
EDIT:
If I skip the icons there is no problem. I have also tried going
QListWidgetItem *item = new QListWidgetItem(act_filename);
ui->listWidget->addItem(item);
item->setIcon(QIcon(act_fullname));
and got no difference.
EDIT 2:
I do not call QObject::moveToThread(QThread*) I don't even use threads (deliberately at least).
Also, the errors appear to come after the loop. I have cout-ed every iteration and the end of the loop and right after my "end loop cout msg" I see that
objc[56963]: Class QCocoaColorPanelDelegate is implemented in both
/Users/Barnabas/QtSDK/Desktop/Qt/4.8.1/gcc/lib/QtGui.framework/Versions/4/QtGui
and
/Users/Barnabas/Programming/Qt/demo_OpenCV-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/demo_OpenCV.app/Contents/MacOS/../Frameworks/QtGui.framework/Versions/4/QtGui.
One of the two will be used. Which one is undefined.
Here, too, I do not use QCocoaColorPanelDelegate. I don't even know what it is ... :(
But here is my more detailed code:
boost::filesystem::path p("/path/to/dir");
if(boost::filesystem::is_directory(p))
{
for(boost::filesystem::directory_iterator it(p); it!=boost::filesystem::directory_iterator(); ++it)
{
if(it->path().extension().string()==".png")
{
std::cout<< it->path() <<std::endl;
QString item_name( it->path.stem().c_str() );
QString screen_file( it->path.c_str() );
QListWidgetItem *item = new QListWidgetItem(item_name);
QIcon *icon = new QIcon(screen_file);
item->setIcon(*icon); // if I comment this one out, everything is fine.
ui->imageList->addItem(item);
}
}
}
I have also tested it with a single .png and the image was displayed properly in the list but crash followed with the very same messages.
I have finally found the solution: manually removed the Debug and the Release directories.
For those whose similar problem is not solved by this see: this link.

QLabel setText not displaying text immediately before running other method

I have a basic label that is supposed to indicate to the user that the program is searching directories for several seconds. So it goes like...
self.label.setText(QString("Searching..."))
# method to search directories goes here
self.label.setText(QString("Search Complete"))
My problem is that the label never displays "Searching...". The execution always seems to jump straight to run the method to scan directories, and then the label text is set to "Search Complete" after the method which scans directories has finished.
I'd be grateful if someone could please explain why this is happening or suggest a better way to resolve the problem.
many thanks
Your "method to search directories" is blocking the GUI hence QLabel is not able to update the text. You can make your search routine asynchronous or go the easy way and force QLabel to update itself:
self.label.setText(QString("Searching..."))
self.label.repaint()
# method to search directories goes here
self.label.setText(QString("Search Complete"))
Add include:
#include <qapplication.h>
Let Qt process events:
self.label.setText(QString("Searching..."))
qApp->processEvents();
Note: repaint() was not necessarie.
In PyQt5, you don't need to use QString :
self.label.setText("Searching...")
self.label.repaint()
self.label.setText("Search Complete")

Is there a way to wait for and get a key press from a (remote) terminal session?

I've been playing around with some ANSI stuff (like colors etc.) in java and php (from scratch) and I'm trying to find a way to basically wait for a key press. I'd like to have something like the following pseudo code at the end of my main event loop:
If (KeyPressed)
Begin
var event = new KeyboardEvent();
event.Key = ReadKey();
this.BubbleEvent(event);
End
But everything I've been trying over the last couple days fails because the key presses only become available on STDIN after the user has pressed enter.
It doesn't matter much what language you answer in, but java, php, plain old c or c# would be nicest, and I cannot use any really spiffy library stuff because I need to port it to all four of those languages... I need this to work over a telnet or ssh connection, but my research so far suggests it is impossible unless you're working on the local machine.
Please prove me wrong.
The curses function cbreak(3) will disable line-buffering and erase/kill handling. You can do this yourself with stty(1) if you really want.
When your program dies and leaves the terminal in cbreak mode, you can usually use either stty sane or reset to bring the terminal back to a reasonable state.
From within Perl, you can use either the Term::ReadKey or the Curses module to manipulate the terminal. See the Term::ReadKey(3pm) or Curses(3pm) manpage for details.
From within C, you can use either ioctl(2) calls on the terminal device to turn on cbreak mode, or you can use curses. See the ncurses(3) manpage for details.
I know, this is an old thread, but I could not find a suitable answer anywhere else. So with some help from the senior programmers of my company I came up with this:
private void waitKeypress() throws IOException
{
System.in.read();
while ( System.in.available() > 0 )
{
System.in.read();
}
}
The part reading as much input as is available solved my problem that when used multiple times, "System.in.read()" alone does not always wait.
For me this does the trick, I use it like this:
doSomething();
waitKeypress();
doNextThing();
Hope it helps.
Kind regards,
Ralph

Resources