473,395 Members | 1,537 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,395 software developers and data experts.

scanline equivalent

What's the best way to scan in the standard input line by line in C? I
need to signal the end of input after two \n 's have been inputted.

The only solution I right now to deal with it is to scan in each
character, concatenate it to the previous characters entered, and stop
the input when the last four characters and \n\n. Is there a simpler
way to do this?

Sep 23 '06 #1
8 4242
cr*******@gmail.com writes:
What's the best way to scan in the standard input line by line in C? I
need to signal the end of input after two \n 's have been inputted.

The only solution I right now to deal with it is to scan in each
character, concatenate it to the previous characters entered, and stop
the input when the last four characters and \n\n. Is there a simpler
way to do this?
If you can assume an upper bound on the length of each line, use
fgets().

If you need to handle arbitrarily long lines, you might consider
something like our own CBFalconers ggets(), available at
<http://cbfalconer.home.att.net/download>.

Incidentally, you wrote "when the last four characters and \n\n" (I
presume you meant "are" rather than "and"). '\n' represents a single
newline character.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 23 '06 #2

cr*******@gmail.com wrote:
What's the best way to scan in the standard input line by line in C? I
need to signal the end of input after two \n 's have been inputted.

The only solution I right now to deal with it is to scan in each
character, concatenate it to the previous characters entered, and stop
the input when the last four characters and \n\n. Is there a simpler
way to do this?
I see that you want to parse the http/sip protocol headers. But there
is \r\n at eacj end of the line rather than \n\n. And at the end of the
protocol header there would be \r\n\r\n rather that \n\n\n\n.

Anyway, there is no c function that uses readline kind of functionality
and one need to craft it.

What I did was take the input into a buffer and look for \r\n using
strstr function. Then I copy the contents to another buffer and use
strstr function again after 2 bytes (\r\n). If the strstr function
returns null, it could be that there is more input needed. So move the
remaining buffer to the start of the buffer and append the input.
Restart the check. At each stage check for \r\n\r\n which means the end
of the headers.

It is not efficient but it solved my purpose :)

If you got confused check for the apache source code for http parser.
It does almost the same mechanism.

-kondal

Sep 23 '06 #3
I see that you want to parse the http/sip protocol headers. But there
is \r\n at eacj end of the line rather than \n\n. And at the end of the
protocol header there would be \r\n\r\n rather that \n\n\n\n.
Mmm no, actually I need the \n\n. I'm working a project for school..

What I did was take the input into a buffer and look for \r\n using
strstr function. Then I copy the contents to another buffer and use
strstr function again after 2 bytes (\r\n). If the strstr function
returns null, it could be that there is more input needed. So move the
remaining buffer to the start of the buffer and append the input.
Restart the check. At each stage check for \r\n\r\n which means the end
of the headers.
How do I take the input into a buffer? Could you show me an example of
what you mean by the whole process?

Sep 23 '06 #4

cr*******@gmail.com wrote:
I see that you want to parse the http/sip protocol headers. But there
is \r\n at eacj end of the line rather than \n\n. And at the end of the
protocol header there would be \r\n\r\n rather that \n\n\n\n.

Mmm no, actually I need the \n\n. I'm working a project for school..

What I did was take the input into a buffer and look for \r\n using
strstr function. Then I copy the contents to another buffer and use
strstr function again after 2 bytes (\r\n). If the strstr function
returns null, it could be that there is more input needed. So move the
remaining buffer to the start of the buffer and append the input.
Restart the check. At each stage check for \r\n\r\n which means the end
of the headers.

How do I take the input into a buffer? Could you show me an example of
what you mean by the whole process?
That in the Keith Thompson's post. you can use fgets, fread functions.

Think with a cool mind, you'll get it :) best of luck.

-kondal

Sep 23 '06 #5
If you can assume an upper bound on the length of each line, use
fgets().
Ahh. I thought it said upper bound on length of the whole input, not
each line. Now I get it. *Sigh*

Sep 23 '06 #6
cr*******@gmail.com wrote:
>
What's the best way to scan in the standard input line by line in C?
line_to_string() can be used to scan standard input line by line.

http://groups.google.com/group/comp....694880f515e317
I need to signal the end of input after two \n 's have been inputted.
line_to_string.c ends if either
the first character from standard input is '\n',
or if two successive '\n' characters are input.

--
pete
Sep 23 '06 #7
If you can assume an upper bound on the length of each line, use
fgets().
Actually, the specs require that there are NO bounds on the length of
the each line. So while fgets() worked I did have to scan character by
character. I converted the character to a string, realloced the space
for my variable holding all the input that has come in so far to allow
the next char/string, and concatenate the new input to the existing
input. Then I checked if the new total input contains the string
"\n\n" in it and if it does, I stop scanning & remove one of the \n.

Sep 23 '06 #8
cr*******@gmail.com writes:
>If you can assume an upper bound on the length of each line, use
fgets().
I wrote the above. Please don't snip attribution lines.
Actually, the specs require that there are NO bounds on the length of
the each line. So while fgets() worked I did have to scan character by
character. I converted the character to a string, realloced the space
for my variable holding all the input that has come in so far to allow
the next char/string, and concatenate the new input to the existing
input. Then I checked if the new total input contains the string
"\n\n" in it and if it does, I stop scanning & remove one of the \n.
A requirement to handle unbounded input lines doesn't mean you have to
read the input character by character. For example, you can still use
fgets(), but if the result doesn't end in a '\n' you can tell that
you've read only a partial line; you can then call fgets() again, and
realloc() your result buffer, until you have a complete line.

Or you can use one of the many pre-written routines that handle all
this for you, such as ggets().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 23 '06 #9

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

Similar topics

14
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server...
2
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. ...
3
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
1
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different...
6
by: Frank Rachel | last post by:
So I can focus on the correct areas of research, I was wondering if someone could give me the .NET equivelents for this J2EE architecture: JSP's make calls to local JavaBean Controls. The...
1
by: the G | last post by:
I'm trying to create an asp page for Scanline from Foundstone (http://www.foundstone.com/index.htm?subnav=resources/navigation.htm&subcontent=/resources/proddesc/scanline.htm) with the asp below....
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
10
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.