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 b开发者_Python百科ut 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.
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!
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.
精彩评论