i am using a named pipe for IPC on a Debian system. I will be sending some data as a set of strings from a bash script to a background running process written in C code.
The data i want to send is four strings eg accountid, firstname,surname, description. Currently i am sending the data as a char array separated by spaces from my bash script.
echo "accountid firstname surname description" >$pipe
In the background process i read the pipe data like this into char array ‘datain’
res = read(pipe_fd, datain, BUFFER_SIZE);
then i am just iterating over the array looking for spaces
eg
char* p = datain;
char accountid[80];
char firstname[80];
// extract the accountid
while(p!='')
{
accountid = p;
++p;
}
++p;
while(p!='')
{
firstname = p;
++p;
}
etc….
This method seems a bit crude however my programming skills are not that good so i was wondering if there was a better strategy for transferring this set of data over a named pipe in Linux.
Thanks
A pipe (named or not) is a stream of bytes. If you were using the same language on both sides, there might be a better way of sending structured data. In your situation, a manual encoding and decoding, like you’re doing, is by far the easiest solution.
Don’t use spaces to separate fields that may contain spaces, such as people’s names. Use
:, like/etc/passwd.In C,
readis hard to use, because you have to decide on a buffer size in advance and you have to call it in a loop because it may return less than the buffer size on a whim. The functions fromstdio.h(that operate on aFILE*rather than a file descriptor) are easier to use but still require work to handle long lines. If you don’t care about portability outside Linux, usegetline:Then use
strchrto locate the:s in the line. (Don’t be tempted to usestrtok, it’s only suitable for whitespace-separated fields that can’t be empty.)