D - scanf("%.*s",str) revisited
- Andrew Edwards (15/15) Feb 15 2004 Every now and then this topic rears it's ugly head but there seem to be
-
Ben Hinkle
(22/22)
Feb 15 2004
"Andrew Edwards"
wrote in message - Andrew Edwards (8/33) Feb 15 2004 No didn't try that! Trying now! Sorry...Doesn't work.
- Sam McCall (3/11) Feb 16 2004 Should that be %.*s or %s?
- Andrew Edwards (6/18) Feb 16 2004 Yes! It should be %.*s, but since that hadn't worked in former attempts,...
-
Ben Hinkle
(14/14)
Feb 16 2004
"Sam McCall"
wrote in message - Vathix (12/36) Feb 15 2004 &str is very different from cast(char*)str, and cast(char*)str is also
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);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
Feb 15 2004
char[] str; stdin.scanf("%.s",&str); printf(str); results in: C:\>test Error: Access ViolationShould that be %.*s or %s? I don't know, just wondering... Sam
Feb 16 2004
Sam McCall wrote: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. Andrewchar[] str; stdin.scanf("%.s",&str); printf(str); results in: C:\>test Error: Access ViolationShould that be %.*s or %s? I don't know, just wondering... Sam
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