reading stream up to next delimiter

S

seema_coma

I am working under a Linux environment, and I want to
"read stream up to next delimiter". It looks like linux
doesnt't have a function such as bgets()
All i need is,
reading characters from stream into buffer until either
count is exhausted or one of the characters in breakstring
is encountered in the stream. The read data is terminated
with a null byte (' \0') and a pointer to the trailing null
is returned. If a breakstring character is encountered,
the last non-null is the delimiter character that terminated
the scan. Any library function available to achieve it
how to do it??
thanks in advance
seema
 
C

Chris Croughton

I am working under a Linux environment, and I want to
"read stream up to next delimiter". It looks like linux
doesnt't have a function such as bgets()

Nor does standard C, which is what's on topic here.
All i need is,
reading characters from stream into buffer until either
count is exhausted or one of the characters in breakstring
is encountered in the stream. The read data is terminated
with a null byte (' \0') and a pointer to the trailing null
is returned. If a breakstring character is encountered,
the last non-null is the delimiter character that terminated
the scan. Any library function available to achieve it
how to do it??

No, but it's easy enough to code portably:

#include <string.h>
#include <stdio.h>

/**
* Get a buffer from a stream until either the length is met or
* a character in breakstr is found. The buffer will be nul
* terminated.
*
* \param fp stream pointer
* \param buff buffer pointer
* \param len size of the buffer including trailing nul
* \param breakstr string of characters to act as trailing delimiters
*
* \return number of characters in buffer, excluding trailing nul
*/
int getToDelimiter(FILE *fp, char *buff, int len, const char *breakstr)
{
int count = 0;
int ch;
while (count < len-1 && (ch = fgetc(fp)) != EOF)
{
*buff[count++] = ch;
if (strchr(breakstr, ch))
break;
}
*buff[count] = '\0';
return count;
}

(Feel free to use it, mess with it or whatever. Note that as written if
a nul (0) character is read from the stream it will always be treated as
a terminator -- why this happens and how it can be corrected is left as
an exercise for the reader. I give no warranty of fitness for purpose,
safety in mission-critical applications, or anything else; if it causes
you computer to catch fire, your pet gerbil to die, your car to break
down, or starts a global thermonuclear war I'm not responsible, I wasn't
there, it wasn't me it was some other cat, and anyway I meant to do
it. I didn't spell check it either, any spells are your problem, I'm
not a witch...)

Chris C
 
R

Robert B. Clark

I am working under a Linux environment, and I want to
"read stream up to next delimiter". It looks like linux
doesnt't have a function such as bgets()
All i need is,
reading characters from stream into buffer until either
count is exhausted or one of the characters in breakstring
is encountered in the stream. The read data is terminated
with a null byte (' \0') and a pointer to the trailing null
is returned. If a breakstring character is encountered,
the last non-null is the delimiter character that terminated
the scan. Any library function available to achieve it

What's wrong with fgetc()?
 
C

chming

*buff[count++] = ch;
.... ...
*buff[count] = '\0';

why use *buff[] instead of buff[]?
 
C

Chris Croughton

*buff[count++] = ch;
... ...
*buff[count] = '\0';

why use *buff[] instead of buff[]?

Please include enough of the message to which you are replying so that
people can see the context, in particular include the name of the person
who wrote the original. Usenet is not a perfect medium, posts get lost
and arrive out of order.

As I wrote the original, I can say that I have no idea why the stars are
there, I just checked and they weren't in the test program from which I
copied the code! I suspect some kind of finger trouble after copying...

(I could claim that I did it deliberately to give the person who asked
the question something to think about <g>...)

Chris C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top