Thread overview
scanf("%.*s",str) revisited
Feb 15, 2004
Andrew Edwards
Feb 16, 2004
Ben Hinkle
Feb 16, 2004
Andrew Edwards
Feb 16, 2004
Sam McCall
Feb 16, 2004
Andrew Edwards
Feb 16, 2004
Ben Hinkle
Feb 16, 2004
Vathix
February 15, 2004
Every now and then this topic rears it's ugly head but there seem to be no real solution for to the problem. I'm trying to scan a string (char[]) from a text document or from stdin. What is the correct way to do this? I am aware of the readLine and readString functions available in stream.d but this is not exactly what I'm looking for. Additionally I  could:

char[] str;
char[80] s;
scanf("%s",cast(char*)s);

str = toStringz(s);

but this does not alleviate the problem. Since dynamic string (char[]) guards against array overrunning, it is my first choice when considering string input. I think this is a problem that needs to be remedied, especially for novice programmers (not to say that we are the only ones that use such features), prior to v1.0?
February 16, 2004
"Andrew Edwards" <remove_ridimz@remove_yahoo.com> wrote in message
news:c0ol66$28jl$1@digitaldaemon.com...
| Every now and then this topic rears it's ugly head but there seem to be
| no real solution for to the problem. I'm trying to scan a string
| (char[]) from a text document or from stdin. What is the correct way to
| do this? I am aware of the readLine and readString functions available
| in stream.d but this is not exactly what I'm looking for. Additionally I
|   could:
|
| char[] str;
| char[80] s;
| scanf("%s",cast(char*)s);
|
| str = toStringz(s);
|
| but this does not alleviate the problem. Since dynamic string (char[])
| guards against array overrunning, it is my first choice when considering
| string input. I think this is a problem that needs to be remedied,
| especially for novice programmers (not to say that we are the only ones
| that use such features), prior to v1.0?

Did you try the scanf method in std.stream instead of the std.c.scanf?
 stdin.scanf("%.s",&str);


February 16, 2004
Ben Hinkle wrote:
> "Andrew Edwards" <remove_ridimz@remove_yahoo.com> wrote in message
> news:c0ol66$28jl$1@digitaldaemon.com...
> | Every now and then this topic rears it's ugly head but there seem to be
> | no real solution for to the problem. I'm trying to scan a string
> | (char[]) from a text document or from stdin. What is the correct way to
> | do this? I am aware of the readLine and readString functions available
> | in stream.d but this is not exactly what I'm looking for. Additionally I
> |   could:
> |
> | char[] str;
> | char[80] s;
> | scanf("%s",cast(char*)s);
> |
> | str = toStringz(s);
> |
> | but this does not alleviate the problem. Since dynamic string (char[])
> | guards against array overrunning, it is my first choice when considering
> | string input. I think this is a problem that needs to be remedied,
> | especially for novice programmers (not to say that we are the only ones
> | that use such features), prior to v1.0?
> 
> Did you try the scanf method in std.stream instead of the std.c.scanf?
>  stdin.scanf("%.s",&str);
> 
> 
No didn't try that! Trying now! Sorry...Doesn't work.

char[] str;
stdin.scanf("%.s",&str);
printf(str);

results in:

C:\>test
Error: Access Violation
February 16, 2004
Ben Hinkle wrote:

> "Andrew Edwards" <remove_ridimz@remove_yahoo.com> wrote in message
> news:c0ol66$28jl$1@digitaldaemon.com...
> | Every now and then this topic rears it's ugly head but there seem to be
> | no real solution for to the problem. I'm trying to scan a string
> | (char[]) from a text document or from stdin. What is the correct way to
> | do this? I am aware of the readLine and readString functions available
> | in stream.d but this is not exactly what I'm looking for. Additionally I
> |   could:
> |
> | char[] str;
> | char[80] s;
> | scanf("%s",cast(char*)s);
> |
> | str = toStringz(s);
> |
> | but this does not alleviate the problem. Since dynamic string (char[])
> | guards against array overrunning, it is my first choice when considering
> | string input. I think this is a problem that needs to be remedied,
> | especially for novice programmers (not to say that we are the only ones
> | that use such features), prior to v1.0?
> 
> Did you try the scanf method in std.stream instead of the std.c.scanf?
>  stdin.scanf("%.s",&str);
> 


&str is very different from cast(char*)str, and cast(char*)str is also very different from str.

Stream's scanf seems to be broken. When I do this:
stdin.scanf("%3s", cast(char*)s);
it will keep on writing past 3 characters. The C scanf from std.c.stdio handles it correctly, but I can't get it to read the string length from the stack like we do with printf.


-- 
Christopher E. Miller
www.dprogramming.com
irc.dprogramming.com #D
February 16, 2004
> char[] str;
> stdin.scanf("%.s",&str);
> printf(str);
> 
> results in:
> 
> C:\>test
> Error: Access Violation
Should that be %.*s or %s?
I don't know, just wondering...
Sam
February 16, 2004
Sam McCall wrote:
>> char[] str;
>> stdin.scanf("%.s",&str);
>> printf(str);
>>
>> results in:
>>
>> C:\>test
>> Error: Access Violation
> 
> Should that be %.*s or %s?
> I don't know, just wondering...
> Sam
Yes! It should be %.*s, but since that hadn't worked in former attempts, I'll give any suggestion a try, maybe he stumbled onto something I overlooked. As it were, that wasn't the case. The std.c.scanf() never has never worked, and it seems that steam.scanf() broke along the way.

Andrew
February 16, 2004
"Sam McCall" <tunah.d@tunah.net> wrote in message
news:c0q2mq$1g60$1@digitaldaemon.com...
| > char[] str;
| > stdin.scanf("%.s",&str);
| > printf(str);
| >
| > results in:
| >
| > C:\>test
| > Error: Access Violation
| Should that be %.*s or %s?

oops. that should be %.*s


| I don't know, just wondering...
| Sam