I\'m writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I\'m using EOF (Ctrl-Z or Ctrl-D) to end t
You could use the null character ('\0'
) to separate the variables. Various UNIX tools (e.g. find
) are capable of separating their output items in this way, which would suggest that it's a fairly standard method.
Another advantage of this is that you can read the stream into a single buffer and then create an array of char*
s to point to the individual strings, and each string will be correctly '\0'
-terminated without you having to change anything in the buffer manually. This means less memory allocation overhead, which may make your program run noticeably faster depending on how many variables you're reading. Of course, this is only necessary if you need to hold all the variables in memory at the same time — if you're dealing with them one-at-a-time, you don't get this particular advantage.