Jump to page: 1 2
Thread overview
Can I output strings using core.stdc.stdio?
Dec 22, 2020
Godnyx
Dec 22, 2020
Godnyx
Dec 23, 2020
Paul Backus
Dec 23, 2020
Godnyx
Dec 23, 2020
Mike Parker
Dec 23, 2020
Godnyx
Dec 23, 2020
Ferhat Kurtulmuş
Dec 23, 2020
Ferhat Kurtulmuş
Dec 23, 2020
Godnyx
Dec 23, 2020
Ali Çehreli
Dec 23, 2020
Godnyx
Dec 23, 2020
drug
Dec 23, 2020
Adam D. Ruppe
Dec 23, 2020
Godnyx
Dec 23, 2020
Godnyx
Dec 22, 2020
Dave P.
Dec 22, 2020
Godnyx
Dec 22, 2020
Dave P.
Dec 23, 2020
Godnyx
December 22, 2020
Is there a way? If not then how std.stdio does it?
December 22, 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 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.
December 22, 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.


December 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:
>> 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.

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?
December 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:
>> 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?

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);


December 23, 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:
>> 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.

toStringz works just fine on variables that can't be read at compile time. You must be doing something else to trigger that error.
December 23, 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:
>> 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.
>
> toStringz works just fine on variables that can't be read at compile time. You must be doing something else to trigger that error.

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?
December 23, 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:
>> 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?
>
> 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);

They is another problem. See the reply I did to Paul Backus
December 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.
December 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 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.

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?
« First   ‹ Prev
1 2