Incomplete words read from file

pascal111 judas.the.messiah.111 at gmail.com
Thu Nov 18 01:21:00 UTC 2021


On Thursday, 18 November 2021 at 00:42:49 UTC, Ali Çehreli wrote:
> On 11/17/21 3:46 PM, 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?
>
> D assumes UTF-8 encoding by default. If the file is not UTF-8, 
> std.encoding.transcode may be useful:
>
>   https://dlang.org/library/std/encoding/transcode.html
>
> Of course, the encoding of the file must be known. However, I 
> think your file is already UTF-8.
>
> > 
> "https://i.postimg.cc/rpP7dQYH/Screenshot-from-2021-11-18-01-40-43.png"
>
> The characterns indeed look correct. I wonder whether the lines 
> don't fit your terminal's width and the terminal is wrapping 
> them?
>
> If you want to wrap the lines programmatically, there 
> std.string.wrap:
>
>   https://dlang.org/phobos/std_string.html#.wrap
>
> Because we've talked about parts of your program earlier, I 
> take liberty to comment on it. :) Then I will show an 
> alternative version below.
>
> > '''d
> >
> > // D programming language
> >
> > import std.stdio;
> > import std.string;
> >
> > int main()
> > {
> >
> > string s;
>
> It is a general guideline that variables should be defined as 
> close to their first use as possible. This allows for more 
> readable, maintainable, and refactorable code.
>
> > char[] f;
>
> I was able to make this a string in the program below by 
> calling the no-parameter version of readln().
>
> >
> >
> > try{
> > write("Enter file name and path: ");
> > readln(f);
> > f=strip(f);}
>
> Because errors can occur in other parts of the program as well, 
> you can wrap the whole code in a try block.
>
> >
> > catch(Exception err){
> > stderr.writefln!"Warning! %s"(err.msg);}
>
> It is better to either return with a non-zero error code here 
> or do something about the error. For example:
>
>   writeln("Using the default file.")
>   f = "my_default_file".dup;
>
> But I think it is better to return 1 there.
>
> >
> >
> > File file = File(f, "r");
> >
> > while (!file.eof()) {
>
> There is byLine (and byLineCopy) that produce a file 
> line-by-line, which you can use here as well.
>
> >        s = chomp(file.readln());
> >        writeln(s);
> >     }
> >
> > file.close();
>
> Although harmless, you don't need to call File.close because it 
> is already called by the destructor of File.
>
> >
> > return 0;
> >
> > }
> >
> >
> > '''
>
> Here is an alternative:
>
> import std.stdio;
> import std.string;
>
> int main() {
>   try {
>     printFileLines();
>
>   } catch(Exception err){
>     stderr.writefln!"Warning! %s"(err.msg);
>     return 1;
>   }
>
>   return 0;
> }
>
> void printFileLines() {
>   write("Enter file name and path: ");
>   string f = strip(readln());
>
>   File file = File(f, "r");
>
>   foreach (line; file.byLine) {
>     const s = chomp(line);
>     writeln(s);
>   }
> }
>
> Ali


I fixed the code like this and it worked without breaking words, 
but this time it shows single lines as if the normal context is a 
poem. Can we fix this or the terminal will force us and make 
wrapping for lines?

"https://i.postimg.cc/FHQFPgm8/Screenshot-from-2021-11-18-03-16-41.png"


import std.stdio;
import std.string;
import std.process : pipeShell, Redirect, wait;
import std.format;

int main() {

   try {
     printFileLines();

   } catch(Exception err){
     stderr.writefln!"Warning! %s"(err.msg);
     return 1;
   }


   return 0;
}

void printFileLines() {

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


   write("Enter file name and path: ");
   string f = strip(readln());

   File file = File(f, "r");

   foreach (line; file.byLine) {
     const s = chomp(line);
     fmt.stdin.writeln(s);
   }
  }









More information about the Digitalmars-d-learn mailing list