Thread: open a console from a GUI in c++

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    35

    open a console from a GUI in c++

    Please can someone give me an advice on how to open a console from a Gui interface to redirect stdio.
    I need of more than one terminal to redirect in different consoles various output.
    A source code snippet, or a library will be appreciated.
    Thanks in advance
    Lollo

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can probably "popen" an "xterm" (or whatever terminal window you would like).

    Is this for debug purposes, or for your actual application?

    In an application, I would prefer to see either a separate window owned by the app, or a sub-window within the app itself.

    If it's for debug purposes, why not just open a file (/tmp/myapp-log.txt or some such) , an then you open a terminal yourself, and use "tail -f" to see the output of the application.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    Very thanks for the answer...
    I need it for debug, but in real time. My application is a communication protocol and I need to see all bytes arrived. I have multiple of this protocol in seceral channel (multiple serial interface), so I need multiple consoles to see all data arrived and sent.

    Lollo

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So log all data from one serial port in one file, e.g. "myapp-ttyS1.log", and use "tail -f myapp-ttyS1.log" to read the file. Open as many terminal windows as you need...

    "tail -f" will show you the data in real-time [as near as it matters to a human]. You may have to place "fflush(logfile)" when logging, so that the data is going into the log-file "immediately", particularly if the data is coming in relatively infrequently.

    Putting a time-stamp on each log-entry would help indicate the timing of the data itself.

    I can assure you that this model of debugging will be much easier to implement, and just as useful as a method that opens a new terminal window and produces output there - with the big difference that you can actually "look back" in the log-file, whilst if something has gone past your history buffer in the terminal window, it's gone...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    It's really a good advice!!!
    so, I've another problem..I need to format and decorate with others informations the data arrived and sent to render it much more readable. How I can print in that terminals using printf or similar?

    lollo

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    Excuse me...may answer is stupid... I've not understand all the thinks you tell me!
    If I write the log file I see all I wont

    thanks
    Lollo

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use fprintf() to format the output - it works exactly like printf, except there is another parameter in the beginning, which is a FILE *, which is where the output goes. Do "man fprintf" to get more info.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    Thanks salem, but that is valid for windows programming...I'm programming in Linux.
    Lollo

    for Matsp: thanks, it solve entirely my needs...great!!! ...Unfortunately I'm novice in Linux programming
    Last edited by lollobrigido; 01-17-2008 at 05:05 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, if you feel more comfortable with using cout, you can of course use the logfile as a fstream.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Thanks salem, but that is valid for windows programming...I'm programming in Linux.
    So you are </sheepish>

    Perhaps googling for "linux pseudo terminal" would find something to your liking. I'm sure something is possible, it's just a question of "how easy" it is for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    @Salem: I actually think my suggestion of
    Code:
    popen("xterm");
    is the way to do this, if that's what you REALLY want to do - but as explained, it's probably just as easy to manually start a terminal window, and use tail -f to provide the output of the data into the terminal window. It removes the issue of duplicating handles, forking, calling popen, and making all of this conditional so that it doesn't appear in the final code. Whilst adding the fprintf()/stream output will still remain the a similar effort either way. [Obviously, this may need to be conditional too - but again, that's the same whichever way the output is presented to the programmer/debugger/user].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I just thought of something else:

    Logging to a SINGLE file may actually be at least as good, for several reasons.
    1. It will be possible to see if one ports input is interfering with another [in the sense, at least, that the messages from one port is shown next to another ports messages - so you can start to suspect things going wrong when you see that port 1 and port 2 receives a message in short succession and after that it goes wrong].
    2. If the output format is such that the port is a distinct part of the output [e.g. starting with a formatted port number], it's entirely possible to use grep to filter the output. For example, we can do:
    "tail -f somelogfile|grep ^Port01:", // Show messages to port 1 only.
    or
    "tail -f somelogfile|grep -v ^Port0[12]:" // Show messages NOT to port 1 or 2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    the last advice is very useful for me! thanks again..
    Are you expert in bisonc++ too? if yes I've some question about...

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Before this moment, I hadn't even heard of bisonc++, so no, can't say I know anything about it.

    But post your question [in a new thread?], and perhaps someone else with experience with yacc, bison, bisonc++ or similar will be able to answer your question.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DOS / Linux Console based GUI?
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 09-14-2002, 12:46 AM
  2. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 06:03 PM
  3. .NET And GUI Programming :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2002, 04:22 PM
  4. GUI Programming :: C++ Exclusive
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2002, 03:22 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM