473,395 Members | 1,742 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,395 software developers and data experts.

dynamic mem

Hi all...

i have a problem to receive string from keyboard which is very
long...

i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may even
more with all the characters like tab, space, newline .. i do not want
to use fixed or static array.

int main()
{
char *ptr;
scanf("%s",ptr);
}

this is not working ..if it is work it fetching upto white space
occured.. i need to fetch more and more .. how can i do that? Please
explain and help me..

Thank you,
Ganesh.

Apr 24 '07 #1
5 1182
On 24 Apr, 10:41, gNash <ganeshamu...@gmail.comwrote:
Hi all...

i have a problem to receive string from keyboard which is very
long...
You obviously need a shorter keyboard :-)
i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may even
more with all the characters like tab, space, newline .. i do not want
to use fixed or static array.
Then you'll have to be very clever...
int main()
{
char *ptr;
scanf("%s",ptr);
So you've told scanf() to read stdin looking for a blank terminated
string, and it should put the data at the address contained in "ptr",
which you haven't initialised. Is this wise?
}

this is not working ..if it is work it fetching upto white space
That's what you told scanf() to do.

In general don't use scanf().... See for example http://c-faq.com/stdio/scanfprobs.html
occured.. i need to fetch more and more .. how can i do that? Please
explain and help me..
There is no standard mechanism to do what you want but many people
have produced their own solutions - for example Chuck Falconer's ggets
(http://cbfalconer.home.att.net/download/) or Richard Heathfields
fgetline (http://www.cpax.org.uk/prg/writings/fgetdata.php).
Apr 24 '07 #2
gNash wrote:
Hi all...

i have a problem to receive string from keyboard which is very
long...

i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may even
more with all the characters like tab, space, newline .. i do not want
to use fixed or static array.

int main()
{
char *ptr;
scanf("%s",ptr);
}

this is not working ..if it is work it fetching upto white space
occured.. i need to fetch more and more .. how can i do that? Please
explain and help me..
What you have is known as undefined behaviour. You are reading data to
a location pointer by an uninitialised pointer.

You must allocate some memory for your input. Using scanf is unwise for
the very reason you mention, you don't know how much data is to be input
and no matter how big your buffer, it could be overrun. Use fgets to
read a maximum number of characters into a fixed size buffer.

--
Ian Collins.
Apr 24 '07 #3
gNash wrote:
i have a problem to receive string from keyboard which is very
long...
Get a shorter keyboard.

(C doesn't know about "keyboards". It does know about `stdin`, though.)
i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may even
more with all the characters like tab, space, newline .. i do not want
to use fixed or static array.
What exactly are you trying to read? Because it sounds to me as
though you want to read everything, not even stopping at newlines.
In which case, `fread` is your chap: he can read as much as you
ask for (up to whatever's available).
int main()
{
char *ptr;
scanf("%s",ptr);
}
Well, I can't see how you ever thought that would work, since `ptr`,
which is supposed to point to somewhere big enough to hold whatever
`scanf` reads, doesn't point anywhere at all.

You'll need to allocate store using `malloc` and, if it turns out
that there's more you want to read, make it bigger using `realloc`.
Both of these should be described in your C book and whatever
online documentation you have to hand. It's not hard if you pay
attention to the details.

--
"We live for the One, you die for the One." Unsaid /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Apr 24 '07 #4
gNash wrote:
>
Hi all...

i have a problem to receive string from keyboard which is very
long...

i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may even
more with all the characters like tab, space, newline .. i do not want
to use fixed or static array.

int main()
{
char *ptr;
scanf("%s",ptr);
}

this is not working ..if it is work it fetching upto white space
occured.. i need to fetch more and more .. how can i do that? Please
explain and help me..
http://www.mindspring.com/~pfilandr/...ine/get_line.c

--
pete
Apr 24 '07 #5
gNash wrote:
>
i have a problem to receive string from keyboard which is very
long...

i have a pointer variable. using this how can i get variable
length of character streams. the streams in terms of 10000 may
even more with all the characters like tab, space, newline ..
i do not want to use fixed or static array.
See: <http://cbfalconer.home.att.net/download/ggets.zip>

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 25 '07 #6

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

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.