473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reading stream up to next delimiter

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

Nov 14 '05 #1
4 5483
On 6 Jan 2005 03:54:14 -0800, se********@yahoo.co.in
<se********@yahoo.co.in> wrote:
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
Nov 14 '05 #2
On 6 Jan 2005 03:54:14 -0800, se********@yahoo.co.in wrote:
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()?

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
Nov 14 '05 #3
*buff[count++] = ch;
.... ...
*buff[count] = '\0';

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

Nov 14 '05 #4
On 6 Jan 2005 09:32:09 -0800, chming
<ch****@gmail.com> wrote:
*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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
23
by: shank | last post by:
I have the below code found on an ASP site. <% arrName = Split(Request("TextArea"),",") %> <% For i = LBound(arrName) To UBound(arrName) Response.Write "ID: " & arrName(i) & "<br>" Next %>
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
4
by: Fresh_Air_Rider | last post by:
Hi In the "good old" Classic ASP days, I used to stream records from a SQL Server database out to the user's browser in CSV format by using a combination of COALESCE and the ADODB.Stream object....
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
6
by: python | last post by:
Is there an elegant way to unget a line when reading from a file/stream iterator/generator? By "unget" I mean a way to push back a line into the source stream and backtrack the...
10
by: puzzlecracker | last post by:
Say I want to arrange bytes in the internal buffer in a certain way. I receive those bytes in the socket. One solution is to read in socket in pieces: byte buffer = new byte; int index = 0;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.