getting variable length strings from stdin

S

siliconwafer

Hi All,
I want to take a string from stdin(say login Name).But I don't want a
static array of fixed size.I don't want to restrict user to a
perticular sizeof login name.
So I want to allocate memory dynamically to hold the name depending on
size of name.
One way of doing is:
malloc(strlen(gets(char*))+1);
But I read in forum that gets is unsafe and fgets requires a fixed size
which I don't want the user to restrict to.
How can I get away with the problem?
By the way,what is the max size of a login name & password for popular
websites & mail sites?
-Siliconwafer.
 
E

Emmanuel Delahaye

siliconwafer wrote on 18/08/05 :
I want to take a string from stdin(say login Name).But I don't want a
static array of fixed size.I don't want to restrict user to a
perticular sizeof login name.

It's possible with a smart combination of realloc() and fgets() (or
fgetc()).

Do your best, and post your code if you are stuck.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
K

Keith Thompson

siliconwafer said:
I want to take a string from stdin(say login Name).But I don't want a
static array of fixed size.I don't want to restrict user to a
perticular sizeof login name.

For a user name, that's probably overkill. You can easily allocate a
200- or 1000-character buffer; there's probably no point in allowing
user names longer than that. If you use fgets() you can easily detect
whether the user has entered a line longer than the limit (check
whether there's a newline at the end of the string); you can treat
that as an error condition, slurp and discard the rest of the line,
and try again.

But ...
So I want to allocate memory dynamically to hold the name depending on
size of name.
One way of doing is:
malloc(strlen(gets(char*))+1);
But I read in forum that gets is unsafe and fgets requires a fixed size
which I don't want the user to restrict to.

Correct. Never use gets().
How can I get away with the problem?

By repeatedly reading input (perhaps using fgets()) and invoking
realloc() as needed to extend the buffer.

Our own CBFalconer has written the freely available ggets(); see
<http://cbfalconer.home.att.net/download/>. He also provides a link
to fgetline said:
By the way,what is the max size of a login name & password for popular
websites & mail sites?

No idea. 8 characters is a common limit on some systems, but that's
certainly not an upper bound.
 
D

Default User

siliconwafer said:
Hi All,
I want to take a string from stdin(say login Name).But I don't want a
static array of fixed size.I don't want to restrict user to a
perticular sizeof login name.
So I want to allocate memory dynamically to hold the name depending on
size of name.
One way of doing is:
malloc(strlen(gets(char*))+1);

This doesn't make any sense. You'd already have to have a buffer of the
correct size for gets() to read into. I don't think you understand the
C input functions. You should stop what you are doing until you do.
But I read in forum that gets is unsafe and fgets requires a fixed
size which I don't want the user to restrict to.

Yes, gets() is unsafe because you can't limit the amount of input and
can overrun the buffer.
How can I get away with the problem?

Stop programming by rumor, for one thing. There are no standard C
functions to do what you want. Look into a third party solution, such
as CB Falconers ggets() found in his download site:

http://cbfalconer.home.att.net/download/index.htm

By the way,what is the max size of a login name & password for popular
websites & mail sites?

Off-topic.


Brian
 
S

SM Ryan

# Hi All,
# I want to take a string from stdin(say login Name).But I don't want a
# static array of fixed size.I don't want to restrict user to a
# perticular sizeof login name.
# So I want to allocate memory dynamically to hold the name depending on
# size of name.

// Read characters up to next NL or EOF and returns in a string.
// Caller frees returned value with free().
// Returns NULL if the file was EOF and no characters were read.

char *rdl(FILE *f) {
int m = 0,n = 0; char *s = 0;
int ch;
while (ch=fgetc(f), ch!=EOF && ch!-='\n') {
if (n+2>m) {m = 2*n+2; s = realloc(s,m);}
s[n++] = ch; s[n] = 0;
}
if (s) s = realloc(s,n+1);
return s;
}
 
J

Jack Klein

This doesn't make any sense. You'd already have to have a buffer of the
correct size for gets() to read into.

No, no, NO, NO!!!

The horror, the agony, the sheer terror. Please retract the above
statement.

There is no such thing as "a buffer of the correct size" for calling
gets().
 

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
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top