Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2015 The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Hi, Tell me, please, why this code works correctly always: import std.stdio; int n; readf("%s\n", &n); string s, t; readf("%s\n%s\n", &s, &t); And this code works correctly is not always: import std.stdio; readf("%s\n", &n); char[200010] s, t; scanf("%s%s", s.ptr, t.ptr); Data is entered only in this format (n - the length of strings str1 and str2, 1 <= n <= 200000; only lowercase letters): n str1 str2 For example: 5 cater doger |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Saturday, 21 March 2015 at 08:37:59 UTC, Dennis Ritchie wrote: > Tell me, please, why this code works correctly always: [...] > And this code works correctly is not always: > > import std.stdio; > > readf("%s\n", &n); > > char[200010] s, t; > scanf("%s%s", s.ptr, t.ptr); Please go into more detail about how it doesn't work. |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Saturday, 21 March 2015 at 12:08:05 UTC, anonymous wrote: > Please go into more detail about how it doesn't work. Task: http://codeforces.com/contest/527/problem/B?locale=en It works: char[200010] s, t; s = readln.strip; t = readln.strip; http://codeforces.com/contest/527/submission/10377392?locale=en It doesn't always work: char[200010] s, t; scanf("%s%s", s.ptr, t.ptr); http://codeforces.com/contest/527/submission/10376852?locale=en P.S. I can't copy test №23 completely. |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | In C++ it is fully working: char s[200005], t[200005]; scanf("%s%s", s, t); http://codeforces.com/contest/527/submission/10376381?locale=en |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote:
> In C++ it is fully working:
>
> char s[200005], t[200005];
> scanf("%s%s", s, t);
Indeed.
Generate a 100000-character string:
-----
import std.range, std.stdio;
void main () {'a'.repeat (100000).writeln;}
-----
Try to copy it with D scanf and printf:
-----
import std.stdio;
void main () {
char [100000] a;
scanf ("%s", a.ptr);
printf ("%s\n", a.ptr);
}
-----
Only 32767 first characters of the string are actually copied.
|
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote: > On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote: >> In C++ it is fully working: >> >> char s[200005], t[200005]; >> scanf("%s%s", s, t); > > Indeed. And why in D copied only the first 32767 characters of the string? I'm more days couldn't understand what was going on... > Generate a 100000-character string: > ----- > import std.range, std.stdio; > void main () {'a'.repeat (100000).writeln;} > ----- > > Try to copy it with D scanf and printf: > ----- > import std.stdio; > void main () { > char [100000] a; > scanf ("%s", a.ptr); > printf ("%s\n", a.ptr); > } > ----- > > Only 32767 first characters of the string are actually copied. Thank you very much. |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | On 2015-03-21 at 16:05, Ivan Kazmenko wrote:
> Generate a 100000-character string
>[...]
> Try to copy it with D scanf and printf:
> -----
> import std.stdio;
> void main () {
> char [100000] a;
> scanf ("%s", a.ptr);
> printf ("%s\n", a.ptr);
> }
> -----
>
> Only 32767 first characters of the string are actually copied.
In what universe?! Which OS, compiler and architecture?
|
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to FG | On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: > In what universe?! Which OS, compiler and architecture? On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: > In what universe?! Which OS, compiler and architecture? Windows 8.1 x64, dmd 2.066.1: import std.range, std.stdio; void main () { stdout = File("in.txt", "w"); 'a'.repeat(100000).writeln; } import std.stdio; import std.cstream; void main () { freopen("in.txt", "r", din.file); freopen("out.txt", "w", dout.file); char[100000] a; scanf("%s", a.ptr); int lenA; foreach (i; 0 .. 100000) { if (a[i] == 'a') ++lenA; printf("%c", a[i]); } printf("\n%d\n", lenA); // 32767 } By the way, in Ubuntu 14.04 LTS (dmd 2.066.1) everything works fine: #!/usr/bin/rdmd import std.range, std.stdio; void main () { stdout = File("in.txt", "w"); 'a'.repeat(100000).writeln; } #!/usr/bin/rdmd import std.stdio; import std.cstream; void main () { freopen("in.txt", "r", din.file); freopen("out.txt", "w", dout.file); char[100000] a; scanf("%s", a.ptr); int lenA; foreach (i; 0 .. 100000) { if (a[i] == 'a') ++lenA; printf("%c", a[i]); } printf("\n%d\n", lenA); // 100000 } |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Kazmenko | On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote: > Generate a 100000-character string: > ----- > import std.range, std.stdio; > void main () {'a'.repeat (100000).writeln;} > ----- > > Try to copy it with D scanf and printf: > ----- > import std.stdio; > void main () { > char [100000] a; > scanf ("%s", a.ptr); > printf ("%s\n", a.ptr); > } > ----- > > Only 32767 first characters of the string are actually copied. That doesn't happen on linux, but I could reproduce it in wine. Seems to be a bug in the C runtime (snn.lib). I filed an issue: https://issues.dlang.org/show_bug.cgi?id=14315 |
March 21, 2015 Re: The difference in string and char[], readf() and scanf() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On 2015-03-21 at 21:02, Dennis Ritchie wrote: >> In what universe?! Which OS, compiler and architecture? > Windows 8.1 x64, dmd 2.066.1: That's strange. I cannot recreate the problem on Win7 x64 with dmd 2.066.1, neither when compiled for 32- nor 64-bit. I have saved the a's to a file and use input redirect to load it, while the program is as follows: import std.stdio; void main () { char [100000] a; scanf ("%s", a.ptr); printf ("%s\n", a.ptr); } > freopen("in.txt", "r", din.file); No, that approach didn't change the result. I still get 10000. |
Copyright © 1999-2021 by the D Language Foundation