Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Dynamic text with C++?


  • Please log in to reply
9 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:07:09 PM

Posted 22 November 2009 - 01:56 PM

Hey, I just updated my ubuntu and noticed that the built in console writes a percentage and then appends it to a higher one, is there a way of appending a console output to somthing else in C++?

Thanks for any help, Wolfy87.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:09 PM

Posted 22 November 2009 - 03:31 PM

Depends on which I/O system you're using. If you're using the C i/o system, checkout freopen(). I.e. freopen(stdout, ...);

If you're using the C++ I/o system, you set std::cout's std::basic_streambuf to something else. For an example -> http://www.cplusplus.com/reference/iostream/ios/rdbuf/

Hope that helps,
Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:07:09 PM

Posted 22 November 2009 - 04:24 PM

Well that is to do with file io, I didnt make itnvery clear sorry. What I meant was if you do somthing like

cout << "Percentage complete is: " << percentage << "%";

Then could you literally take that line away, change the percentage and then perform the cout again? Or is that sort of thing impossible?

Thankyou for you help.

Edited by Wolfy87, 22 November 2009 - 04:24 PM.


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:09 PM

Posted 22 November 2009 - 09:54 PM

Hmm.. I'm confused. If you have the source of the program like that, you can just write "percentage + 1" ...

Why not just change the point in the program where it outputs the number?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:02:09 PM

Posted 25 November 2009 - 12:33 AM

I usually print a backspace character to erase the previously printed percentage like this
printf("\b"); in C or cout << "\b"; in C++

You are printing 100% (4 characters) so you have to insert 4 backspaces to erase previously printed output. You can even create an advanced function for ding this. You can easily port the code in C++ as well.

Edit : removed full source code, remembering the forum policy.

Edited by Romeo29, 25 November 2009 - 12:44 AM.


#6 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:07:09 PM

Posted 25 November 2009 - 01:33 PM

Thats exactly what I needed thank you very much, I knew you could do some cool things with printf but backspace? Wow.

Thank you for your help you two, Wolfy87.

#7 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:09 PM

Posted 25 November 2009 - 01:39 PM

Again I ask -- why not modify it where the number is output rather than outputting the number and then modifying it later?

If you need to overtype previously written output, there's \b, which is a portable way of doing it. However, if you're targeting a specific platform (Windows or POSIX) you can use native function calls to make it faster.

Unix machines use the ncurses library for managing text output. I'm not familiar with this library, but I do know that's it's intended purpose.

Windows has a whole host of console functions that do the same job: http://msdn.microsoft.com/en-us/library/ms...28VS.85%29.aspx

Hope that helps,
Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#8 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:07:09 PM

Posted 25 November 2009 - 02:19 PM

I see, thanks, although I try to keep it as general as possible because I run a dual boot (XP and Ubuntu) I will probably give it a go.

Thank you, Wolfy87.

#9 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:02:09 PM

Posted 26 November 2009 - 02:12 PM

But Billy what if someone wants to make a program for DOS mode in ANSI C? You don't have Windows API to help ya :thumbsup:

#10 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:12:09 PM

Posted 26 November 2009 - 08:43 PM

Yep :thumbsup: Using native APIs (for either *nix or Windows) is going to be faster, but of course they are not portable solutions. Not saying one method is better than another -- just wanted to point out that faster solutions exist of one is willing to sacrifice portability.

For PEV, it doesn't much matter because PEV is a windows only app. I never intended it to be portable.

Though I do ask... who is writing DOS apps anymore? Considering the last revision to DOS came out in 2000 (eew.. windows ME), you should be able to use EITHER win32 OR ncurses depending on what you want to do. If you want to stick to ANSI -- I understand. It's just not fast enough to implement say -- animations inside the command prompt. (Last time I looked into this, animations were what I was looking to do)

Use the right too for the job. When all you have is a hammer. everything starts to look like a nail. Don't get what I like to call hammer/nail symdrome :flowers:

Have a nice day,

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users