473,387 Members | 1,283 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.

getting variable length strings from stdin

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.

Nov 15 '05 #1
5 4493
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"
Nov 15 '05 #2
"siliconwafer" <sp*********@yahoo.com> 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) ks***@mib.org <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.
Nov 15 '05 #3
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
Nov 15 '05 #4
"siliconwafer" <sp*********@yahoo.com> 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.
Nov 15 '05 #5
On 18 Aug 2005 19:03:07 GMT, "Default User" <de***********@yahoo.com>
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
Nov 15 '05 #6

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

Similar topics

20
by: Mark Hahn | last post by:
Prothon is pleased to announce another major release of the language, version 0.1.2, build 710 at http://prothon.org. This release adds many new features and demonstrates the level of maturity...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
18
by: lawrence | last post by:
If I'm pretty sure there is just one form on the page, can i do this? var myForm = document.forms; If I'm not sure about the form, is it safer to do this? if (document.forms) { var myForm =...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
12
by: leonard.guillaume | last post by:
Hi guys, I use dynamic char arrays and I'm trying to get rid of the garbage in it. Let me show you the code and then I'll explain more in details. ...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
9
by: CapCity | last post by:
We're rewritting an app using C# 2005 and it needs to read files in netCDF format. A dll is available for this and we've had success in calling its functions, unless it updates strings. We have...
4
by: Daryl Lee | last post by:
I am trying to locate all lines in a suite of files with quoted strings of particular lengths. A search pattern like r'".{15}"' finds 15-character strings very nicely. But I have some very long...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.