D - scanf("%.*s",str) revisited
- Andrew Edwards <remove_ridimz remove_yahoo.com> Feb 15 2004
- "Ben Hinkle" <bhinkle4 juno.com> Feb 15 2004
- Andrew Edwards <remove_ridimz remove_yahoo.com> Feb 15 2004
- Sam McCall <tunah.d tunah.net> Feb 16 2004
- Andrew Edwards <remove_ridimz remove_yahoo.com> Feb 16 2004
- "Ben Hinkle" <bhinkle4 juno.com> Feb 16 2004
- Vathix <vathix dprogramming.com> Feb 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?
Feb 15 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);
Feb 15 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);
char[] str; stdin.scanf("%.s",&str); printf(str); results in: C:\>test Error: Access Violation
Feb 15 2004
char[] str; stdin.scanf("%.s",&str); printf(str); results in: C:\>test Error: Access Violation
I don't know, just wondering... Sam
Feb 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
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
Feb 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
Feb 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
Feb 15 2004









Andrew Edwards <remove_ridimz remove_yahoo.com> 