Text formatting

Pages: 12
Hi guys,

I am looking how to format the width of a txt and control the text line to line ( this txt i am gonna read it from the computer )

thx
If you mean outputting text to the screen, then use the <iomanip> library.
the text already exists but i want to output it using a certain width ( max 30 letters in a line )
Yes, the <iomanip> will help you do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

void print30( const string& str )
{
    const size_t MAX_CHARS{ 30 };
    
    size_t i{};
    for( ; i < str.length( ) / MAX_CHARS; i++ )
        cout << str.substr( i * MAX_CHARS, MAX_CHARS ) + "\n";
    
    cout << str.substr( i * MAX_CHARS, string::npos ) + "\n";
}

int main( )
{
    const string str{
        "`Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe."
    };
    
    print30( str );
}


`Twas brillig, and the slithy 
toves Did gyre and gimble in t
he wabe: All mimsy were the bo
rogoves, And the mome raths ou
tgrabe.
thx a lot but i don't want to cut the words

the text is gonna be readed from a file.txt and after formatting it it is gonna be saved in another file.txt
Last edited on
The text is gonna be readed from a file.txt and after formatting it it is gonna be saved in another file.txt

What do you mean? Can you give us a sample text?
cmajor1 wrote:
Ok Let me explain Another Time
At First i am Not gonna write the text but i am gonna Take from a
File.txt after that i need to Change the Format of the text by using
only 30 letters by line without cutting the words for example:
The text is "hello hello hello hello hello hello"
It is gonna be like that : "hello hello hello hello hello"
"hello"
So you see there is two spaces between the last two hello in the First
line
so here is a good example
the text file is this one
https://moodle.uni-due.de/pluginfile.php/768345/mod_resource/content/4/loremipsum.txt

and i have to put it in this form
https://moodle.uni-due.de/pluginfile.php/768943/mod_resource/content/6/loremipsum-40.txt

i am not allowed to cut a word in two line
I don't think those links are visible unless logged in - they don't work for me.

You could copy and paste an example here - use the output tags.
[output]example text here[/output]
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam	nonumy	eirmod tempor invidunt ut labore et dolore  magna aliquyam erat, sed diam voluptua.	At   vero eos et accusam et justo duo dolores   et ea rebum.		Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.



after formatting
Lorem ipsum dolor sit amet,   consetetur 
sadipscing elitr, sed diam nonumy eirmod 
tempor  invidunt  ut  labore  et  dolore  
magna aliquyam erat, sed diam  voluptua. 
At vero eos et accusam et justo      duo 
dolores et ea rebum. Stet clita     kasd 
gubergren, no sea takimata sanctus   est 
Lorem ipsum dolor sit amet. Lorem  ipsum 
dolor sit amet, consetetur    sadipscing 
elitr, sed diam nonumy eirmod     tempor 
invidunt  ut  labore  et  dolore   magna  
aliquyam erat, sed diam voluptua.     At 
vero eos et accusam et justo duo dolores 
et ea rebum. Stet clita kasd  gubergren, 
no sea takimata sanctus est Lorem  ipsum 
dolor sit amet.
@cmajor1
Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo
dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est
Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing
elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores
et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum
dolor sit amet.

Sorry, we do not understand this paragraph.
and after formatting
it have to be savec in a text file another time
ok let say i have this text

RFC:  791







                           INTERNET PROTOCOL


                         DARPA INTERNET PROGRAM

                         PROTOCOL SPECIFICATION



                             September 1981

							 1.  INTRODUCTION

1.1.  Motivation

  The Internet Protocol is designed for use in interconnected systems of
  packet-switched computer communication networks.  Such a system has
  been called a "catenet" [1].  The internet protocol provides for
  transmitting blocks of data called datagrams from sources to
  destinations, where sources and destinations are hosts identified by
  fixed length addresses.  The internet protocol also provides for
  fragmentation and reassembly of long datagrams, if necessary, for
  transmission through "small packet" networks.


and it have to be formatted like that (only 20 letters in a line couting the spaces )

RFC:  791   INTERNET  
PROTOCOL       DARPA       
INTERNET     PROGRAM     
PROTOCOL
SPECIFICATION
September  1981   1.  
INTRODUCTION    1.1.    
Motivation       The       
Internet Protocol is 
designed for use  in 
interconnected
systems           of           
packet-switched
computer
communication
networks.   Such   a   
system   has    been   
called  a  "catenet"  
[1].  The   internet  
protocol    provides    
for     transmitting     
blocks    of    data    
called     datagrams     
from   sources    to   
destinations,  where  
sources          and          
destinations     are     
hosts identified  by 
fixed         length         
addresses.       The       
internet    protocol    
also  provides   for  
fragmentation    and    
reassembly  of  long  
datagrams,        if        
necessary,       for       
transmission through 
"small       packet"       
networks. 
Thanks for the examples.
That's interesting. The "after formatting" is not only limited to the required width, but it also has additional whitespace inserted in order to make each line the same length (text justification).

I'd suggest just fitting it within the width first. The spacing can come later.
So what you need to do is start from about character 31 in the source text. If that is a space, you can print the first 30 characters as they are. If not, you need to search backwards one character at a time (position 30, position 29, position 28 and so on), until you find a space. That gives you a point at which the string can be split. Use the substr() function to extract that part and output it. Basically you just repeat this process over and over until all the text has been processed. Of course you need to start from the point just after the last string ended each time.

I've not explained that very well, but it's hard to explain without writing the actual code for you.
Last edited on
Another way to approach it is to read one word at a time from the source text.
Gradually build up a string to be output by adding each word to the end of a string. If the length would be greater than 30, instead of adding the word, write out the string, then empty that string and start to add words again until the length would be greater than 30 ... and so on.
exactly that what i am looking for

and the text gonna be taken by using

myfile.open("text.txt");
and so on

and after formatting it is gonna be saved in another text file
that means my result have to be a file too i don't need to output it on the screen
my program have to give me a file.txt as return

give a text.txt ---> formatting ---> text_formatted.txt


i don't know how to convert this text file to a string so that i can work on it
someone can help ?
Last edited on
Well, I tried playing around with this a little bit.

First, to read from the file I suggest something like this:
1
2
3
4
5
6
7
8
9
    std::ifstream myfile("text.txt");

    std::string line;
    std::string word;

    while (myfile >> word)
    {

    }


Inside the body of the while loop you might do such things as
 
    line += space + word;
or simply
 
    line = word;

as appropriate. You will need to check the length of the line and of the word using the .size() function, to check whether adding the word to the line will break the limit (20 or 40 characters as required). If it will be too long, then output the line to the output file. ... and so on ... I leave the details to you.
Last edited on
i tried me too and i found something like that

cpp.sh/94ecb

what i need now how to insert ur part into my program
the problem is how to add this space ??
Pages: 12