By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

scanline equivalent

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.