www.digitalmars.com         C & C++   DMDScript  

D - scanf("%.*s",str) revisited

reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
"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
next sibling parent reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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
parent reply Sam McCall <tunah.d tunah.net> writes:
 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
next sibling parent Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
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
prev sibling parent "Ben Hinkle" <bhinkle4 juno.com> writes:
"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
prev sibling parent Vathix <vathix dprogramming.com> writes:
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