digitalmars.D.learn - Can I output strings using core.stdc.stdio?
- Godnyx (1/1) Dec 22 2020 Is there a way? If not then how std.stdio does it?
- Godnyx (3/4) Dec 22 2020 I should mention that I want to use it in a variable that can't
- Paul Backus (4/8) Dec 22 2020 toStringz works just fine on variables that can't be read at
- Godnyx (6/16) Dec 23 2020 Yep and I find it out! It won't work with templates and/or
- Mike Parker (4/9) Dec 23 2020 Please show the code that's causing the error. Without it, all
- Godnyx (18/27) Dec 23 2020 Yep that's the best thing I can do! Code:
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (9/40) Dec 23 2020 I didn't dive into your use case, but you should use static
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (9/19) Dec 23 2020 and better:
- Godnyx (5/26) Dec 23 2020 This method works but I won't let me use arguments other than
- =?UTF-8?Q?Ali_=c3=87ehreli?= (35/39) Dec 23 2020 I replaced for with foreach and it worked (and I passed "prompt").
- Godnyx (22/61) Dec 23 2020 Probably the best solution! It also let's me use types other than
- drug (23/25) Dec 23 2020 Just fix your typos:
- Adam D. Ruppe (7/11) Dec 23 2020 Putting some `else` in there would help too to ensure your thing
- Godnyx (5/16) Dec 23 2020 I will probably but I probably won't add array, class support.
- Godnyx (4/29) Dec 23 2020 Damn I'm fucking BLIND! Anyway I changed it to foreach (x; args)
- Dave P. (15/16) Dec 22 2020 I assume you’re asking this because you don’t have access to
On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:Is there a way? If not then how std.stdio does it?I should mention that I want to use it in a variable that can't be read at compile time so .toStringz is not working for me.
Dec 22 2020
On Tuesday, 22 December 2020 at 21:26:37 UTC, Godnyx wrote:On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:toStringz works just fine on variables that can't be read at compile time. You must be doing something else to trigger that error.Is there a way? If not then how std.stdio does it?I should mention that I want to use it in a variable that can't be read at compile time so .toStringz is not working for me.
Dec 22 2020
On Wednesday, 23 December 2020 at 04:02:54 UTC, Paul Backus wrote:On Tuesday, 22 December 2020 at 21:26:37 UTC, Godnyx wrote:Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases?On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:toStringz works just fine on variables that can't be read at compile time. You must be doing something else to trigger that error.Is there a way? If not then how std.stdio does it?I should mention that I want to use it in a variable that can't be read at compile time so .toStringz is not working for me.
Dec 23 2020
On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases?Please show the code that's causing the error. Without it, all anyone can do is keep making suggestions that *might* be the problem. With the code, someone can point to it exactly.
Dec 23 2020
On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker wrote:On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:Yep that's the best thing I can do! Code: import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { for (ulong i = 0; i < args.length; i++) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } } void main() { string h = "World!"; string w = "World!"; put(h, w); } I'm getting two errors. First that i can't be read at compile time and second that I don't initialize the function right. So I know I'm doing something wrong but I don't know why... Any ideas?Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases?Please show the code that's causing the error. Without it, all anyone can do is keep making suggestions that *might* be the problem. With the code, someone can point to it exactly.
Dec 23 2020
On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker wrote:I didn't dive into your use case, but you should use static foreach in this case: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } }On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:Yep that's the best thing I can do! Code: import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { for (ulong i = 0; i < args.length; i++) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } } void main() { string h = "World!"; string w = "World!"; put(h, w); } I'm getting two errors. First that i can't be read at compile time and second that I don't initialize the function right. So I know I'm doing something wrong but I don't know why... Any ideas?Yep and I find it out! It won't work with templates and/or variadic function parameters. It says that the variable can't be read at compile time (so I can't cast it) or it will work but it will give me a segmentation fault (lol hello C). Any idea why this is happening in those cases?Please show the code that's causing the error. Without it, all anyone can do is keep making suggestions that *might* be the problem. With the code, someone can point to it exactly.
Dec 23 2020
On Wednesday, 23 December 2020 at 09:40:27 UTC, Ferhat Kurtulmuş wrote:On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:and better: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); } }[...]I didn't dive into your use case, but you should use static foreach in this case: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } }
Dec 23 2020
On Wednesday, 23 December 2020 at 09:42:42 UTC, Ferhat Kurtulmuş wrote:On Wednesday, 23 December 2020 at 09:40:27 UTC, Ferhat Kurtulmuş wrote:This method works but I won't let me use arguments other than string. Ali gave a solution that will solve this limitation! See me reply below!On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:and better: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); } }[...]I didn't dive into your use case, but you should use static foreach in this case: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); } }
Dec 23 2020
On 12/23/20 1:06 AM, Godnyx wrote:for (ulong i = 0; i < args.length; i++) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); }I replaced for with foreach and it worked (and I passed "prompt"). static foreach would work as well. import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { foreach (arg; args) { if (typeof(arg).stringof == "string") printf("%s\n", arg.toStringz); } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } But it can get better: you don't want to compare typeof with "string" at run time. Instead, there shouldn't be any code generated for the non-string cases. Enter 'static if' and the 'is' expression to feel better. :) import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { static foreach (arg; args) { static if (is (typeof(arg) == string)) { printf("%s\n", arg.toStringz); } } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } Ali
Dec 23 2020
On Wednesday, 23 December 2020 at 09:50:03 UTC, Ali Çehreli wrote:On 12/23/20 1:06 AM, Godnyx wrote:Probably the best solution! It also let's me use types other than string! Example: put("Prompt:", "Hello, my age is: ", 19, true); tho still I can't print anything. This is the code: void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { static if (is(typeof(arg) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(arg) == int)) printf("%i\n", args[i]); static if (is(typeof(arg) == bool)) { if (arg) printf("true"); else printf("false"); } } } void main() { put("Prompt:", "Hello, my age is: ", 19, true); } Any ideas?for (ulong i = 0; i < args.length; i++) { if (typeof(args[i]).stringof == "string") printf("%s\n", args[i].toStringz); }I replaced for with foreach and it worked (and I passed "prompt"). static foreach would work as well. import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { foreach (arg; args) { if (typeof(arg).stringof == "string") printf("%s\n", arg.toStringz); } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } But it can get better: you don't want to compare typeof with "string" at run time. Instead, there shouldn't be any code generated for the non-string cases. Enter 'static if' and the 'is' expression to feel better. :) import core.stdc.stdio : printf; import std.string : toStringz; void put(A...)(string prompt, A args) { static foreach (arg; args) { static if (is (typeof(arg) == string)) { printf("%s\n", arg.toStringz); } } } void main() { string h = "World!"; string w = "World!"; put("prompt", h, w); } Ali
Dec 23 2020
On 12/23/20 3:23 PM, Godnyx wrote:Any ideas?Just fix your typos: ```D import std : printf, toStringz; void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int)) printf("%i\n", args[i]); static if (is(typeof(args[i]) == bool)) { if (args[i]) printf("true"); else printf("false"); } } } void main() { put("Prompt:", "Hello, my age is: ", 19, true); } ``` P.S. replace `arg` by `args[i]`
Dec 23 2020
On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int))Putting some `else` in there would help too to ensure your thing only ever matches one branch. doesn't matter here but will later if you add generic array support. and then there's const etc but that's yet another thing so wait on that till you have the basics down
Dec 23 2020
On Wednesday, 23 December 2020 at 13:55:20 UTC, Adam D. Ruppe wrote:On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:I will probably but I probably won't add array, class support. Probably I'll use this only for debug purposes with std.write. Thanks a lot for everything!!!static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int))Putting some `else` in there would help too to ensure your thing only ever matches one branch. doesn't matter here but will later if you add generic array support. and then there's const etc but that's yet another thing so wait on that till you have the basics down
Dec 23 2020
On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:On 12/23/20 3:23 PM, Godnyx wrote:Damn I'm fucking BLIND! Anyway I changed it to foreach (x; args) to get the args itself. Thanks for anything man! The community is the best thing about D!!! ;)Any ideas?Just fix your typos: ```D import std : printf, toStringz; void put(A...)(string prompt, A args) { static foreach (ulong i; 0..args.length) { static if (is(typeof(args[i]) == string)) printf("%s\n", args[i].toStringz); static if (is(typeof(args[i]) == int)) printf("%i\n", args[i]); static if (is(typeof(args[i]) == bool)) { if (args[i]) printf("true"); else printf("false"); } } } void main() { put("Prompt:", "Hello, my age is: ", 19, true); } ``` P.S. replace `arg` by `args[i]`
Dec 23 2020
On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:Is there a way? If not then how std.stdio does it?I assume you’re asking this because you don’t have access to std.stdio (such as using betterC). The way to do it is to use the %.*s specifier in printf. For example: void print_string(string text){ printf(“%.*s\n”, cast(int)text.length, text.ptr); } The ‘.N' in front of the ’s’ says to not print more than N characters from the char*. using a ‘*’ says that the actual number of characters will be passed as an argument to printf instead of a hardcoded number. This is specified to be an int, so we have to cast the length of the string to int when calling printf. Finally, we need to pass the pointer to the actual character data, thus the text.ptr.
Dec 22 2020
On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote:On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:Lol. Actually I just don't want to use Phobos and trying to stay on core. Unfortunately, my variable can't be read at compile time so I doesn't work. Any other ideas?Is there a way? If not then how std.stdio does it?I assume you’re asking this because you don’t have access to std.stdio (such as using betterC). The way to do it is to use the %.*s specifier in printf. For example: void print_string(string text){ printf(“%.*s\n”, cast(int)text.length, text.ptr); } The ‘.N' in front of the ’s’ says to not print more than N characters from the char*. using a ‘*’ says that the actual number of characters will be passed as an argument to printf instead of a hardcoded number. This is specified to be an int, so we have to cast the length of the string to int when calling printf. Finally, we need to pass the pointer to the actual character data, thus the text.ptr.
Dec 22 2020
On Tuesday, 22 December 2020 at 21:37:23 UTC, Godnyx wrote:On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote:What I wrote should still work in non-betterC as long as you are linking to libc. You can also just write to stdout directly if all you need is to write the string without any extra formatting: fwrite(somestring.ptr, 1, somestring.length, stdout);On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: [...]Lol. Actually I just don't want to use Phobos and trying to stay on core. Unfortunately, my variable can't be read at compile time so I doesn't work. Any other ideas?
Dec 22 2020
On Tuesday, 22 December 2020 at 21:40:15 UTC, Dave P. wrote:On Tuesday, 22 December 2020 at 21:37:23 UTC, Godnyx wrote:They is another problem. See the reply I did to Paul BackusOn Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote:What I wrote should still work in non-betterC as long as you are linking to libc. You can also just write to stdout directly if all you need is to write the string without any extra formatting: fwrite(somestring.ptr, 1, somestring.length, stdout);On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote: [...]Lol. Actually I just don't want to use Phobos and trying to stay on core. Unfortunately, my variable can't be read at compile time so I doesn't work. Any other ideas?
Dec 23 2020