Incomplete words read from file

jfondren julian.fondren at gmail.com
Thu Nov 18 00:39:12 UTC 2021


On Wednesday, 17 November 2021 at 23:46:15 UTC, pascal111 wrote:
> I made small program that shows the content of textual files, 
> and it succeeded to show non-English (Ascii code) language, but 
> in many lines some words are not complete and their rests are 
> in next lines, how can fix it?

there's nothing in your program that breaks lines differently 
from the input. If you've a Unicode-aware terminal it should 
really work as it is. If 'cat Jekyll1' doesn't produce the same 
output as this program... then there must be some right-to-left 
work that needs to happen that I'm aware of.

If what you're wanting to do is to *reshape* text so that it 
prints with proper word-breaks across lines according to the 
current size of the terminal, then you've got to do this work 
yourself. On Unix a simple shortcut might be to print through 
fmt(1) instead:

```d
void main() {
     import std.process : pipeShell, Redirect, wait;

     auto fmt = pipeShell("fmt", Redirect.stdin);
     scope (exit) {
         fmt.stdin.close;
         wait(fmt.pid);
     }

     char[15] longword = 'x';
     foreach (i; 1 .. 10) {
         fmt.stdin.writeln(longword);
     }
}
```

which outputs:

```
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
```

or... oh, there's std.string.wrap. With the same output:

```d
void main() {
     import std.string : wrap;
     import std.stdio : write;

     enum longtext = {
         char[15] longword = 'x';
         string result;
         foreach (i; 1 .. 10)
             result ~= ' ' ~ longword;
         return result;
     }();

     write(longtext.wrap(72));
}
```

These tools might not do what you want with that language, though.


More information about the Digitalmars-d-learn mailing list