Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 25, 2021 Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
For printf() we can use this format `"%.*s", cast(int)s s.length, s.ptr`. When trying to use the same for scanf(), it says that this specifier is invalid. |
January 25, 2021 Re: Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rempas | On Monday, 25 January 2021 at 09:16:11 UTC, Rempas wrote: > For printf() we can use this format `"%.*s", cast(int)s s.length, s.ptr`. When trying to use the same for scanf(), it says that this specifier is invalid. The * has a different meaning for scanf than for printf ([1] vs [2]). There's also the issue that a string is immutable(char)[]. If you really, really, really, must use scanf: ``` char[bufSize] buf; scanf("%s", buf.ptr); ``` But please don't. This is D, not 1990s C. [1] https://www.cplusplus.com/reference/cstdio/scanf/ [2] https://www.cplusplus.com/reference/cstdio/printf/ |
January 25, 2021 Re: Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Monday, 25 January 2021 at 10:33:14 UTC, Mike Parker wrote:
> On Monday, 25 January 2021 at 09:16:11 UTC, Rempas wrote:
>> For printf() we can use this format `"%.*s", cast(int)s s.length, s.ptr`. When trying to use the same for scanf(), it says that this specifier is invalid.
>
> The * has a different meaning for scanf than for printf ([1] vs [2]).
>
> There's also the issue that a string is immutable(char)[].
>
> If you really, really, really, must use scanf:
>
> ```
> char[bufSize] buf;
> scanf("%s", buf.ptr);
> ```
>
> But please don't. This is D, not 1990s C.
>
> [1] https://www.cplusplus.com/reference/cstdio/scanf/
> [2] https://www.cplusplus.com/reference/cstdio/printf/
Thanks! Actually for some reason. It won't accept a char[size]. I created a heap allocated (with pureFree and pureMalloc) chrar*, then used fgets() and created an empty string and looped through the result adding one by one character until the '\n' which is not included.
|
January 25, 2021 Re: Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rempas | On Monday, 25 January 2021 at 17:38:21 UTC, Rempas wrote:
> On Monday, 25 January 2021 at 10:33:14 UTC, Mike Parker wrote:
>> On Monday, 25 January 2021 at 09:16:11 UTC, Rempas wrote:
>>> [...]
>>
>> The * has a different meaning for scanf than for printf ([1] vs [2]).
>>
>> There's also the issue that a string is immutable(char)[].
>>
>> If you really, really, really, must use scanf:
>>
>> ```
>> char[bufSize] buf;
>> scanf("%s", buf.ptr);
>> ```
>>
>> But please don't. This is D, not 1990s C.
>>
>> [1] https://www.cplusplus.com/reference/cstdio/scanf/
>> [2] https://www.cplusplus.com/reference/cstdio/printf/
>
> Thanks! Actually for some reason. It won't accept a char[size]. I created a heap allocated (with pureFree and pureMalloc) chrar*, then used fgets() and created an empty string and looped through the result adding one by one character until the '\n' which is not included.
char[buffsize] works if buffsize is a compile time constant. Or just char[1024]. İt is a static array, and it's size must be defined at compile time.
|
January 25, 2021 Re: Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ferhat Kurtulmuş | On Monday, 25 January 2021 at 18:28:09 UTC, Ferhat Kurtulmuş wrote:
> On Monday, 25 January 2021 at 17:38:21 UTC, Rempas wrote:
>> On Monday, 25 January 2021 at 10:33:14 UTC, Mike Parker wrote:
>>> [...]
>>
>> Thanks! Actually for some reason. It won't accept a char[size]. I created a heap allocated (with pureFree and pureMalloc) chrar*, then used fgets() and created an empty string and looped through the result adding one by one character until the '\n' which is not included.
>
> char[buffsize] works if buffsize is a compile time constant. Or just char[1024]. İt is a static array, and it's size must be defined at compile time.
Idk, for my it says that you can't pass dynamic arrays to scanf
|
January 25, 2021 Re: Can we use strings with scanf? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rempas | On Mon, Jan 25, 2021 at 06:43:18PM +0000, Rempas via Digitalmars-d-learn wrote: > On Monday, 25 January 2021 at 18:28:09 UTC, Ferhat Kurtulmuş wrote: > > On Monday, 25 January 2021 at 17:38:21 UTC, Rempas wrote: > > > On Monday, 25 January 2021 at 10:33:14 UTC, Mike Parker wrote: > > > > [...] > > > > > > Thanks! Actually for some reason. It won't accept a char[size]. I created a heap allocated (with pureFree and pureMalloc) chrar*, then used fgets() and created an empty string and looped through the result adding one by one character until the '\n' which is not included. > > > > char[buffsize] works if buffsize is a compile time constant. Or just char[1024]. İt is a static array, and it's size must be defined at compile time. > > Idk, for my it says that you can't pass dynamic arrays to scanf Dynamic arrays in D are "fat" pointers, they have a .ptr and .length, but scanf expects C-style bare pointers, so you have to specify .ptr in order to pass the pointer to scanf. Be aware, however, that doing this means you no longer have D's bounds-checking protection, and scanf may cause buffer overruns if the user gives bad input. T -- Being able to learn is a great learning; being able to unlearn is a greater learning. |
Copyright © 1999-2021 by the D Language Foundation