将字符串写入长度超过 4095 个字符的文件
我正在构建一个分布式消息系统,供用户在不同的 Linux 终端上相互发送消息。我正在寻找的问题场景是当我尝试将字符串写入超过 4095 字节/字符的文本文件时。该字符串将根据用户输入生成。我读到 ISO C99 标准的最大字符串大小是 4095 字节/字符。我是否应该限制用户只能输入小于 4095 字节的字符串?我知道简单的聊天消息通常很短,但我关心的场景如下。
用户尝试从某处复制并粘贴摘录,然后将其发送给另一个用户。如果消息长度超过 4095 个字节,那么它将截断第 4095 个字符之后的字符。因此,用户将仅接收第一部分。
我对此不确定,但我想知道如果字符串长度超过 4095 个字符,我应该采取增加堆栈大小的方法,或者是否有另一种方法可以通过在接收字符串时将其拆分然后写入来解决此问题多个字符串一块一块地添加到文件中。
I am building a distributed messaging system for users to send messages to each other on different linux terminals. The scenario that I am looking at for my question is when I try to write a string to a text file that is over 4095 bytes/chars. This string will be generated from user input. I have read that the ISO C99 standard for max string size is 4095 bytes/chars. Should I limit the user to only inputting a string that is less than 4095 bytes? I know simple chat messages are usually short but the scenario that I am concerned with is the following.
The user tries to copy and paste an excerpt from somewhere and then send it to another user. If the message was longer than 4095 bytes then it would truncate the chars after the 4095th char. Thus the user would only receive the first portion.
I’m not sure about this but I was wondering I should take the route of increasing my stack size if the string is longer than 4095 chars or is there another way around this by somehow splitting up the string as i take it in and then writing multiple strings to the file piece by piece.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该限制是针对字符串文字的,例如
"hello world"
。这不是对以编程方式构造的字符串的一般限制。您可以使字符串远大于 4095 字节!That limit is on string literals, like
"hello world"
. It's not a general limit on strings you construct programatically. You can make strings much, much larger than 4095 bytes!使用所谓的“缓冲区”并一次写入 4095 个字节。我不是 C 程序员,所以我现在无法想到缓冲输出函数,但它存在。
Use what's called a "buffer" and write 4095 bytes at a time. I'm not a C programmer so I can't think of the buffered output function right now, but it exists.