www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error reading char in read

reply Jean Cesar <jeanzonta777 yahoo.com.br> writes:
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
Feb 17 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 17 2017
parent reply wiki <jeanzonta777 yahoo.com.br> writes:
On Friday, 17 February 2017 at 18:57:55 UTC, Ali Çehreli wrote:
 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
So I executed it here anyway but still it presents arbitrary characters in the char .. What I thought was to create a reader where I could receive, Char, string, int, float, double, real, for my future codes .... I tried doing this to understand how the char reading was, it is preferable to use strings but the language has some specific reader for char? minha frase na captura de char ������������������������
Feb 17 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/17/2017 12:05 PM, wiki wrote:

 So I executed it here anyway but still it presents arbitrary characters
 in the char ..
Right. char[50] is not suitable for user interaction like that. Use string, char[], etc.
 What I thought was to create a reader where I could receive,
 Char, string, int, float, double, real, for my future codes ....
That's already available in many different ways: readf, readln, formattedRead, etc.
 I tried doing this to understand how the char reading was, it is
 preferable to use strings but the language has some specific reader for
 char?
"%c" can be used to read char. (Although, I still think you want to read a string, not a single char.) import std.stdio; void main() { char c; readf("%c", &c); }
 minha frase na captura de char
������������������������
Sorry, Google Translate did not work for that one. :( There is a confusion here. You keep saying 'char' but � is not a char (i.e. one of the 8-bit types). It's the Unicode character "U+FFFD REPLACEMENT CHARACTER", which is represented by 2 chars in D. char is a UTF-8 code unit, not a "character" in the sense that I think you're using it. Ali
Feb 17 2017
parent reply ag0aep6g <anonymous example.com> writes:
On 02/17/2017 09:24 PM, Ali Çehreli wrote:
 It's the Unicode character "U+FFFD
 REPLACEMENT CHARACTER", which is represented by 2 chars in D.
It takes 3 `char`s to represent U+FFFD: void main() { import std.stdio; writeln("\uFFFD".length); /* prints "3" */ }
Feb 17 2017
parent Jean Cesar <jeanzonta777 yahoo.com.br> writes:
On Friday, 17 February 2017 at 21:34:16 UTC, ag0aep6g wrote:
 On 02/17/2017 09:24 PM, Ali Çehreli wrote:
 It's the Unicode character "U+FFFD
 REPLACEMENT CHARACTER", which is represented by 2 chars in D.
It takes 3 `char`s to represent U+FFFD: void main() { import std.stdio; writeln("\uFFFD".length); /* prints "3" */ }
Yes I also think about the possibility of the user to use the char definition to read a sequence of characters, but calm ....
Feb 17 2017