Thread: Word wrap

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Word wrap

    Hi, I'm writing a console application which requires quite large chunks of text to be displayed. When the text is output to the next line of the console individual words are broken up, is there an easy way of getting complete words to wrap onto the next line?

    Thanks, Gareth.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not that I know of. Unless you specfically define it, your console program won't be able to understand the difference between a half of a word and a whole word. The only thing you can do is format it according to the standard demensions of a console window.

    For example, if the standard window is 80 characters wide, you have to stick an endl in your literal constant before it reaches that 80 characters. Even then, it won't look perfect if someone doesn't use standard windows or perhaps a different resolution.

    Maybe someone knows an answer, but that's as far as I can get you.
    Sent from my iPadŽ

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can write a function that accepts a string, a stream, and a column width that intelligently wraps words at the column width. Think of it as a filter between the processing and the output. The actual process isn't that complicated, you just find a span of characters that matches the column width, then walk back and add a newline at the first non-word character (such as whitespace).
    My best code is written with the delete key.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, but do you know the function that returns the width of the screen?
    Sent from my iPadŽ

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yeah, but do you know the function that returns the width of the screen?
    Yes, I do. But since it's not portable, that's why I said that the function takes a column width. Obviously the biggest issue for the OP is to get words to wrap without splitting.
    My best code is written with the delete key.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Gareth,

    Assuming you're writing a Windows console application, maybe you'll find something in the MSDN Console Reference.

    I've done this in a Windows GUI program. There is a WinAPI function (which I can't remember right now) that, given a string and a font, returns the length in pixels. (Of course, there is another function that returns the Window-width in pixels.) There is probably an easier way to do it, assuming your console program uses a fixed-width font.

    [EDIT] - Maybe the Windows console is always 80 columns??? I just opened-up a DOS / console program and I couldn't re-size it... (I'm not sure if this is a true-DOS program or a console program... and this computer doesn't have a compiler...)
    Last edited by DougDbug; 10-14-2005 at 02:11 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    isspace() is useful for this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to a file with word wrap?
    By Djanvk in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2008, 05:09 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM