Error reading char in read

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 17 10:57:55 PST 2017


On 02/17/2017 07:48 AM, Jean Cesar wrote:
> import std.stdio;
> import std.string;
>
>
> auto read(C)(ref C c, char[80] message)
> if (isSomeChar!C) {
>     writef("\n\t%s: ", message);
>     c = strip(readf());
>     readf(" %s", &t);
>     return c;
> }
>
> void main()
> {
>  char[50] message;
>   read(message,"Digite Seu nome: ");
>  writeln(message);
> }
>
> estou tentando fazer um leitor de vetor de char porem esta dando erro

My recommendation is to read some tutorials as there are many large and 
small differences between D, C++, and Java.

Some notes:

- In D, char is UTF-8 code unit. Perhaps you want to read ubyte (or byte)?

- isSomeChar is defined in std.traits. You must import std.traits if you 
want to use isSomeChar:

   https://dlang.org/phobos/std_traits.html

- From the way you call read(), it looks like you want to read a char 
array (vector), not a single char. So, isSomeChar is wrong.

- It is common to use the string type in D. string is an array of 
immutable chars. We can change my read() function to read char[] as well:

- Static arrays like char[50] are different from slices like char[].

The read() examples I had given are incomplete: They should work with 
value types and dynamic strings but not with static arrays. Here is 
another read() that works with char arrays:

import std.stdio;
import std.string;
import std.traits;
import std.range;
import std.algorithm;

auto read(S)(ref S s, string message)
if (isSomeString!S) {
     import std.string : strip;
     writef("%s: ", message);
     s = readln().strip();
     return s;
}

auto read(S)(ref S s, string message)
if (isStaticArray!S &&
     isSomeChar!(ElementType!S)) {

     string tmp;
     read(tmp, message);

     // Clear the array
     s[] = typeof(s[0]).init;

     // Assign without going out of bounds
     const len = min(s.length, tmp.length);
     s[0..len] = tmp[0..len];

     return s;
}

void main()
{
     char[50] message;
     read(message,"Digite Seu nome: ");
     writeln(message);
}

However, you'll see that static arrays may not be suitable for reading 
text, as char[50] will never know how long the actual text is. Here is 
the output:

Digite Seu nome: : Jean
Jean\377\377\377[...]

For that to work, you would have to rely on the old C representation of 
strings with the '\0' character:

     // Assign without going out of bounds
     const len = min(s.length, tmp.length);
     s[0..len] = tmp[0..len];
     const nullChar = min(s.length - 1, len);
     s[nullChar] = '\0';

However, it still doesn't work because char[50] still has 50 characters:

Jean^@\377\377[...]

As you see, you better use 'string' (or its mutable version, char[]) for 
such text.

And again, it will take a long time to go through these basics unless 
you start with some tutorials:

   https://tour.dlang.org/

For strings:

   https://tour.dlang.org/tour/en/basics/alias-strings

Some books:

   https://wiki.dlang.org/Books

Ali



More information about the Digitalmars-d-learn mailing list