Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getting variable length strings from stdin

Reply
Thread Tools

getting variable length strings from stdin

 
 
siliconwafer
Guest
Posts: n/a
 
      08-18-2005
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.

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-18-2005
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"


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-18-2005
"siliconwafer" <(E-Mail Removed)> writes:
> 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, at <http://users.powernet.co.uk/eton/c/fgetdata.html>.

> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-18-2005
siliconwafer wrote:

> 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
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      08-18-2005
"siliconwafer" <(E-Mail Removed)> wrote:
# 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;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Title does not dictate behaviour.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-19-2005
On 18 Aug 2005 19:03:07 GMT, "Default User" <(E-Mail Removed)>
wrote in comp.lang.c:

> siliconwafer wrote:
>
> > 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.


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().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
String exceeding length - Getting absolute string length james.w.appleby@gmail.com Java 5 01-11-2007 12:07 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments