dynamic mem

G

gNash

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

mark_bluemel

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

Ian Collins

gNash said:
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.
 
C

Chris Dollin

gNash said:
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.
 
P

pete

gNash said:
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/C/get_line/get_line.c
 

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
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top