How to use lib function "getline"

C

Chen shuSheng

I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}
 
S

spibou

Chen said:
I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}

getline() is not standard C therefore out of topic here.
You should ask at comp.unix.programmer or
comp.os.linux.development.apps
 
M

Michael Mair

Chen said:
I have a code:

<iostream.h> is not a standard C header.
<OT>It is not even a standard C++ header; <iostream> is.
If you try to use C and C++ together, ask in comp.lang.c++;
in this case, you would not #include <stdlib.h> but
#include <stdlib.h>

int main()

int main (void)
is a little bit more explicit and preferred around here.
{ int max=15;
char line[max];
getline(line,max);

getline() is not a standard C functions. glibc offers it
system("PAUSE");

This gives you no guaranteedly reproducible results
across systems or even implementations on the same system.
return 0;
}

Good of the compiler.
As getline() is not known because there is no prototype in
scope, the compiler assumes (via the old implicit int rule)
that the function returns int.
Get a prototype in scope by including the header that gives
you getline() on your system. Look up the usage of getline.
Your usage suggests that you would rather be content with
fgets() or the non-standard ggets() which, for example, can
be obtained from Chuck Falconer's page:
http://cbfalconer.home.att.net/download/
It is in the public domain and written in standard C.


Cheers
Michael
 
D

Default User

Chen said:
I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
getline() is not standard C therefore out of topic here.
You should ask at comp.unix.programmer or
comp.os.linux.development.apps


However, it is a standard C++ function. Please don't give redirects
unless you are ACTUALLY familiar with the problem.

The correct group is comp.lang.c++.




Biran
 
J

jacob navia

Chen said:
I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}
You have to find the header file that has the
prototype of the "getline" function.

This is a common problem for beginners, and it is easily solved.
Just look for the documentation of your getline function.
Under Unix this happens mostly with

man getline

or

info getline

Under windows you have to use some kind of IDE and
press F1 with the cursor in the "getline" function call.
 
C

Chen shuSheng

Under windows you have to use some kind of IDE and
press F1 with the cursor in the "getline" function call.

I have a reference book "C/C++ lib" which tells me that this pototype is in
"iostream.h". But now it is not there. My IDE is DEV-C++.And do not have a
reference for "getline". Could you tell me what is your IDE?
 
S

spibou

Default said:
Chen said:
I have a code:
---------------------------
#include <iostream.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
getline() is not standard C therefore out of topic here.
You should ask at comp.unix.programmer or
comp.os.linux.development.apps


However, it is a standard C++ function. Please don't give redirects
unless you are ACTUALLY familiar with the problem.

It appears to me that both you and I are guessing at
what the original poster's problem is. It could be that
your guess is correct and mine isn't but unless Chen
elaborates you can't be sure what the correct redirection
is.

And needless to say that I don't offer suggestions unless
I believe (perhaps mistakenly) that I understand the
problem.
 
J

jacob navia

Chen said:
"jacob navia" <[email protected]>
??????:[email protected]...




I have a reference book "C/C++ lib" which tells me that this pototype is in
"iostream.h". But now it is not there. My IDE is DEV-C++.And do not have a
reference for "getline". Could you tell me what is your IDE?
Dev C++ is a really bad quality IDE. It delivers nothing like
really good help system...

If you want to go on with C instead of using C++ you can
just do:

#include <stdio.h>
#include <stdlib.h>

int main()
{ int max=15;
char line[max];
fgets(line,max,stdin);
system("PAUSE");
return 0;
}
 
K

Keith Thompson

Chen shuSheng said:
I have a code:

This is not a standard C header. (C++, maybe?)
#include <stdlib.h>

int main()

Better: "int main(void)".
{ int max=15;
char line[max];

This is a variable-length array. This is standard in C99, but not all
compilers support it.

<OT>It's also standard in C++, but if you're trying to write a C++
program you should ask in comp.lang.c++. said:
getline(line,max);

There is no "getline" function in standard C.
system("PAUSE");

This is not portable; many systems don't have an external command
called "PAUSE".
 
M

Michael Mair

Chen said:
"jacob navia" <[email protected]>
??????:[email protected]...

I have a reference book "C/C++ lib" which tells me that this pototype is in
"iostream.h". But now it is not there.

Jacob already answered the rest of your question.
There are two languages, C and C++. Parts of C++ _look_ similar
to C and C++ defines its relationship to C with respect to
linkage. Do not mix the two of them unless you know exactly what
you are doing.

The C++ standard library incorporates the C90 standard library
but by other header names in order to disambiguate between C
and C++ headers.

You obviously got information about a method of a C++ class,
not a function. You probably would have to use it like
cin.getline() or something like that. As it is C++, you
ought to ask for advice in comp.lang.c++ if you want to write
C++.

If you want to write C, then there is no standard C library
function getline() and you can go with fgets() or ggets() as
I mentioned elsethread if you want to stay on the safe side.
Or, if you want to use getline(), you have to obtain it from
somewhere and use it according to its documentation.


Cheers
Michael
 
I

Ian Collins

Keith said:
Chen shuSheng said:
I have a code:


This is not a standard C header. (C++, maybe?)

#include <stdlib.h>

int main()


Better: "int main(void)".

{ int max=15;
char line[max];


This is a variable-length array. This is standard in C99, but not all
compilers support it.

<OT>It's also standard in C++, but if you're trying to write a C++
program you should ask in comp.lang.c++.</OT>
No, it's an error in C++, where VLAs are absent.
 
C

Chen shuSheng

Keith Thompson said:
There is no "getline" function in standard C.

I see this function in K&R classic "The C programming language". All code
are below.
-----------
#include <ctype.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#define MAXLINE 100
/* rudimentary calculator */
/* atof: convert string s to double */
double atof(char s[]);

int main(void)
{
double sum;
char line[MAXLINE];
int getline(char line[], int max);
sum = 0;
while(getline(line,MAXLINE)> 0)
printf("\t%g\n", sum += atof(line));
return 0;
}

double atof(char s[])
{
double val, power;
int i, sign;
for (i = 0; isspace(s); i++); /* skip white space */
sign = (s == '-') ? -1 : 1;
if (s == '+' || s == '-')
i++;
for (val = 0.0; isdigit(s); i++)
val = 10.0 * val + (s - '0');
if (s == '.')
i++;
for (power = 1.0; isdigit(s); i++) {
val = 10.0 * val + (s - '0');
power *= 10;
}
return sign * val / power;
}
---------------
That is why I assume "getline" could be find somewhere in C.Also your name
is alike originator of this book. And I confused right now with thinking I
see the author.
After you test this code, you will find a message: "no reference of
getline". Is it the author writing a wrong code?
 
K

Keith Thompson

Chen shuSheng said:
I have a reference book "C/C++ lib" which tells me that this pototype is in
"iostream.h". But now it is not there. My IDE is DEV-C++.And do not have a
reference for "getline". Could you tell me what is your IDE?

I wouldn't trust anything with "C/C++" in the title. C and C++ are
two different languages. Decide which language you want to use. If
your book is that unclear about the difference, get a better book.
 
I

Ian Collins

Chen said:
I see this function in K&R classic "The C programming language". All code
are below.

This is an obsolete C++ header, why are you using it in what is supposed
to be C?
 
C

Chen shuSheng

Ian Collins said:
This is an obsolete C++ header, why are you using it in what is supposed
to be C?

Sorry, "#include <iostream.h>" is added by me. Because I debug this program.
Without that, Have a look again.
 
K

Keith Thompson

Ian Collins said:
Keith Thompson wrote: [...]
<OT>It's also standard in C++, but if you're trying to write a C++
program you should ask in comp.lang.c++.</OT>
No, it's an error in C++, where VLAs are absent.

D'oh! That will teach me not to post off-topic information.

(No, it probably won't.)
 
I

Ian Collins

Chen said:
I see this function in K&R classic "The C programming language". All code
are below.

Look back a couple of pages, you will see getline was the previous example!
 
K

Keith Thompson

Chen shuSheng said:
I see this function in K&R classic "The C programming language". All code
are below.

I don't have my copy of K&R handy at the moment, but ...
-----------
#include <ctype.h>
#include <stdio.h>
#include <iostream.h>

#include <stdlib.h>
#define MAXLINE 100
/* rudimentary calculator */
/* atof: convert string s to double */
double atof(char s[]);

int main(void)
{
double sum;
char line[MAXLINE];
int getline(char line[], int max);

This is a declaration of a function called "getline", but there's no
definition for it. If this appeared in an example in K&R, there would
be a definition for the getline function, possibly in a separate file
(though in that case I'd expect the declaration to be in a header).

[snip]

Not unless you write it yourself.
Also your name is alike originator of this book. And I confused
right now with thinking I see the author.

The book you mentioned, K&R, was written by Brian Kernighan and Dennis
Ritchie. Ken Thompson is another author who has worked with both of
them. I'm not related to him.
After you test this code, you will find a message: "no reference of
getline". Is it the author writing a wrong code?

The code you posted uses a non-standard "getline" function without
defining it. I don't believe that mistake exists in K&R.

Incidentally, *please* indent your code; it's very difficult to read
otherwise.
 
M

Martin Ambuhl

Chen said:
I have a code:

<iostream.h> is not a standard C header. The similarity between
<iostream.h> and the name of a C++ header, <iostream>, suggests that you
may be trying to write C++. Note
1) C and C++ are different languages. C++ has its own newsgroup
<2) Both the C and C++ newsgroups have FAQs. It is impolite to post to a
newsgroup without checking the FAQ first.
3) Both the C and C++ newsgroups are archived at Google groups. It is
impolite to post to a newsgroup without following it for while. This
can be done in something close to real time (as we did in the old days)
or much more quickly with the Google group archives.
4) Before posting to <learn enough C++ so you don't
blunder with said:
#include <stdlib.h>
int main()
{ int max=15;
char line[max];
getline(line,max);
system("PAUSE");
return 0;
}

There is no function getline() in C and you need to learn about using
namespaces if your are going to write in C++. Do so before posting to
<(and don't post anything about C++ here).
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top