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

getline- to limit input (where do the unused characters in stdin go?)

I have been using the getline function from K&R2 for simple character
input.
The prototype is

int getline(char *s, int limit); /* returns 0 to signal EOF */

This function simply calls getchar() in a loop, which breaks if EOF or
too much input occurs (and places necessary '\0').

I always made sure the buffer pointed to by s was large (corresponding
to large limit argument). I only did this thinking that if the user
typed in a lot, then those characters that were not taken in (because
the limit has been reached), then those characters will be sitting in
my input stream waiting to attack the next naive character input.

Today I tested this fear, and it wasn't true. The remaining characters
disappeared when I typed in a lot of junk...

..#include <stdio.h>

..#define MAXLINE 4

..int getline(char line[], int max);

..int main()
..{
.. int length;
.. char line[MAXLINE];
..
.. while((length = getline(line,MAXLINE)) > 0)
.. {
.. puts(line);
.. }
..
.. system("PAUSE");
..
.. return 0;
..}
Sample input/output (my console):

input echo: 12345678
output: 123
input echo: 12345678
output: 123
What I was expecting (which always made me set MAXLINE very high):

input echo: 12345678
output: 123
input echo: 12345678
output: 456

(with 7812345678 still waiting to be read thereafter).

Is there something I am getting wrong? Can I get over my fear and set
MAXLINE to a reasonable value from now on? (Already read the FAQ)
Advanced Thx

Nov 14 '05 #1
1 3055
Luke Wu <Lo***********@gmail.com> wrote:
I have been using the getline function from K&R2 for simple character
input.
The prototype is int getline(char *s, int limit); /* returns 0 to signal EOF */
For others' convenience:
/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}

[snip]
.#include <stdio.h> .#define MAXLINE 4 .int getline(char line[], int max); .int main()
.{
. int length;
. char line[MAXLINE];
.
. while((length = getline(line,MAXLINE)) > 0)
. {
. puts(line);
. }
.
. system("PAUSE");
.
. return 0;
.}
Sample input/output (my console): input echo: 12345678
output: 123
input echo: 12345678
output: 123
What I was expecting (which always made me set MAXLINE very high): input echo: 12345678
output: 123
input echo: 12345678
output: 456 (with 7812345678 still waiting to be read thereafter).
This expectation is incorrect, see below.
Is there something I am getting wrong? Can I get over my fear and set
MAXLINE to a reasonable value from now on? (Already read the FAQ)
Advanced Thx


I have no idea what happens in your case. For reference this is my
output, which is as expected:
$ ./a.out
12345678 [input echo]
123
456
78

[^D]
$

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #2

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

Similar topics

5
by: Frank Schmitt | last post by:
Hi! I've just been bitten by the famous Windows newline issue, i.e. tried to read a textfile written by a windows application on an unix platform. When reading a line with ...
2
by: Donald Canton | last post by:
Hi, My goal is to read a text file consisting of lines containing someone's name followed by one or more numeric values. All fields are tab-delimited. The name could be in any one of the...
2
by: Michael Easterly | last post by:
What is a good way to read a text file and read each line, then assign data to variables? I have this so far, #include <iostream> #include <fstream> using namespace std; string...
1
by: bg | last post by:
hi all, i need to parse a text file, making several passes over it. I'm doing something like this: ifstream input(args); ...... string line; while(getline(input,string)) // first pass works...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
10
by: Aleks Dubinskiy | last post by:
Hi folks, I am well aware of the superiority of using streams and operators << / >> instead of the old-style printf and scanf. However, there is only one obstacle that prevents me from switching...
1
by: yodadbl07 | last post by:
hey ive been messing around with this program to take a user inputed number, put it into an array and then reverse the number. I also want it to omit unecessary 0's at the begging. for example if...
11
by: Peter Olcott | last post by:
Does C++ have anything like this?
20
by: Bill Waddington | last post by:
This must be a FAQ - or several FAQs - but I can't quite seem to pin it down. I need to read a string from stdin which I will then process as digits with sscanf. I need to limit the # of chars...
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: 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
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
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.