gpt4 book ai didi

c# - 当文档建议我不能读取时,为什么我可以使用 Console.ReadLine 读取超过 254 个字符?

转载 作者:行者123 更新时间:2023-12-04 10:37:17 24 4
gpt4 key购买 nike

docsConsole.ReadLine说:

By default, the method reads input from a 256-character input buffer. Because this includes the Environment.NewLine character(s), the method can read lines that contain up to 254 characters. To read longer lines, call the OpenStandardInput(Int32) method.



但是,我可以阅读更多字符就好了。运行一个简单的程序,如:
string s = Console.ReadLine();
Console.WriteLine(s);
Console.WriteLine(s.Length);

Console.ReadKey();

输入如下:
aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj

返回长度为 500 的相同输入。

可运行示例 here .

那么文档是否已经过时,或者这里的“默认”字样是关键?

更新:
Jeremy 发现 source code 中定义的限制为 4096 .

我已经验证 .NET Core 应用程序只会从 stdin 读取前 4094 个字符(不包括换行符)。

就我而言,我实际上有一个 .NET Core 3.1 进程,它启动一个 .NET Framework 4.6 进程,重定向其 StandardOut 和 StandardIn。我已经通过 Console.ReadLine() 验证了 .NET Framework 进程可以成功读取 10 亿个字符。 .NET Core 3.1 进程通过 fwProcess.StandardInput.WriteLine(Serialize(<some stuff>)); 发送框架进程内容的地方

当 .NET Framework 进程替换为 .NET Core 进程时,这也适用。

因此,重定向标准输出/标准输入时似乎 256 个字符的限制不适用,但如果有人可以挖掘解释这一点的明确证明/文档,我将不胜感激。如果还有限制(不包括 OutOfMemory 的情况),但它是 11 亿个字符,我想知道。我还想知道这是否与平台有关(我使用的是 Windows 10)。

如果有帮助,这就是我正在运行的代码。

控制台应用程序1:
ProcessStartInfo processInfo = new ProcessStartInfo {
CreateNoWindow = true,
FileName = "ConsoleApp2.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true
};

StringBuilder s = new StringBuilder();
var proc = Process.Start(processInfo);

int n = 1_000_000_000;
for (int i = 0; i < n; i++)
s.Append("a");

proc.StandardInput.WriteLine(s.ToString());
string s = proc.StandardOutput.ReadLine();
Console.WriteLine(s.Length == n); // logs True

Console.ReadKey();

控制台应用程序2:
string s = Console.ReadLine();
Console.WriteLine(s);

最佳答案

转述自 here :

The default cmd console mode is "ENABLE_LINE_INPUT", which means that when code issues a ::ReadFile call against stdin, ::ReadFile doesn't return to the caller until it encounters a carriage return. But the call to ReadFile only has a limited size buffer that was passed to it. Which means that cmd looks at the buffer size provided to it, and determines based on that how many characters long the line can be... if the line were longer, it wouldn't be able to store all the data into the buffer.



打开时使用的默认缓冲区大小 Console.In现在是 4096 ( source )

该文档是开源的,并且 available here如果您想提交问题或拉取请求。

重定向 StandardOut/StandardIn 时,此限制不适用。来自 here :

The limit is how much memory you pass in.

关于c# - 当文档建议我不能读取时,为什么我可以使用 Console.ReadLine 读取超过 254 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60105591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com