|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
getline to read input from a user
Hi, The gcc compiler has warned about using gets(), so I've been trying my hand at getline. Problem is that I've been able to read from a file, but what I really need is to read from a user's input. I want to use getline like a scanf() command, but I can't figure what to substitute for the fp variable so that it'll be able to read an input from the user. Code:
FILE *fp; char *line=NULL; size_t len=0; //...blah blah... ssize_t read=getline(&line, &len, fp); |
| Sponsored Links | ||
|
|
|
#2
|
|||
|
|||
|
getline is C++ code, this looks like C. The C call that is like getline is fgets.
Another more general point - read is a system API call, so you should reconsider it's use as a variable name. What you want is stdin, that is the name of the standard input device stream in C if you use fgets. fgets(char *buf, size_tn, streams *stdin). cin.getline(char *, streamsize n) is the way do the same thing in C++. len should be a number larger than zero, typically the number of bytes of storage in your line variable. char *line should have memory allocated to it. This advice is not going to help until you go either in a C direction or a C++ direction. |
|
#3
|
|||
|
|||
|
Thanks Jim...I'm using C on Unix.
Is fgets() a good option? Coz we have to specify the size of the characters, right? What if the user types an extremely long line? I've been considering scanf, and apparently it can be used like scanf("%[^/n]",abc); (what's surprising is that abc doesn't need an ampersand preceeding it) Got it from here and there are comments about scanf being vulnerable to buffer overflows. Is that true? So basically, for a user who likes typing long lines, would scanf, fgets or getline be a better option? In the meantime I'll be trying stdin in place of fp, for getline... |
|
#4
|
|||
|
|||
|
There's a teensy problem with using the scanf("%[^/n]...);
What happens is that if I take a line input and create a child process, the child process doesn't even pause at the scanf line. It just proceeds on to create another child process, which does the same thing and keeps going on. Unnerving!!! |
|
#5
|
|||
|
|||
|
I would use scanf() out of habit. If you specify the maximum size of the expected string, e.g. char mystring[21]; scanf("%20c",mystring);, then it should be protected from buffer overflows (note the extra char I allowed for the terminating null).
However if the GNU readline library is available to you you may prefer that; it gives the user full command-line editing facilities at the entry field. I don't understand what you're trying to achieve with the %[^/n]? abc does not need an & because a string, or array of chars, is just a pointer anyway. You could use &abc[0] if you wished, which just means "the address of the first item in the abc array", but the result is the same. |
|
#6
|
|||
|
|||
|
Thanks...scanf seems to have some problems though...on creating a child, if scanf had taken in a line, the child ignores scanf.
|
|
#7
|
|||
|
|||
|
I guess that depends on how you are creating a child and whether or not it has access to the stdin/stdout of the parent process. Without seeing your code it's hard to guess what's wrong there.
|
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| read input file | tjmannonline | UNIX for Dummies Questions & Answers | 2 | 08-05-2008 11:24 AM |
| read user input from within a wile loop that is being fed from below | broli | UNIX for Dummies Questions & Answers | 2 | 01-09-2008 02:19 PM |
| Apply Regex to input read | bonekrusher | UNIX for Dummies Questions & Answers | 3 | 09-23-2007 02:45 PM |
| read input file for batch job | kinmak | Shell Programming and Scripting | 14 | 07-11-2007 07:22 AM |
| ls while read loop - internal read picking up wrong input | dkieran | Shell Programming and Scripting | 2 | 05-14-2007 03:02 PM |