How to take in a string of any size?

Z

zalzon

Do I have to use char * string and then malloc memory to it?

But malloc(size) can only gurantee that size is allocated. What if the
string is more than size?

Is there any way to find the size of the string before taking it in and
THEN malloc the needed amount?

I'm in a rut, help me please :(
 
W

William L. Bahn

You could write it to a file, counting the characters as you go,
and then allocate the memory and read it back in.

You could call realloc(), either every byte or, more sanely, in
reasonable size chunks. Then issue a final realloc() to free up
any straggling memory at the end.

You could allocated blocks in a fixed size and read it into those
blocks. If you get to the end of a block, allocated another
block. Keep track of the number of blocks and maintain an array
or linked list of the pointers. When the string is complete read
it, allocate the memory for the entire string and copy the blocks
to it one by one. Then free up the blocks.
 
Z

zalzon

You could write it to a file, counting the characters as you go,
and then allocate the memory and read it back in.

Except you have to read the string in first before you write it to a file
stream... which leads to the original question of how do you read a string
of unspecified size in and store it.....

You could call realloc(), either every byte or, more sanely, in
reasonable size chunks. Then issue a final realloc() to free up
any straggling memory at the end.

Yea that's what I was kinda fearing I'd have to do.
You could allocated blocks in a fixed size and read it into those
blocks. If you get to the end of a block, allocated another
block. Keep track of the number of blocks and maintain an array
or linked list of the pointers. When the string is complete read
it, allocate the memory for the entire string and copy the blocks
to it one by one. Then free up the blocks.

Good grief... :(
 
P

Profetas

Except you have to read the string in first before you write it to a file
stream... which leads to the original question of how do you read a string
of unspecified size in and store it.....

you may use the a flag in the scanf or the getline function, they both
will allocate the mem for you.
 
J

Jens.Toerring

you may use the a flag in the scanf or the getline function, they both
will allocate the mem for you.

scanf() doesn't allocate any memory at all (the 'a' flag is a GNU
extension that isn't in C89 and even has a different meaning in C99
where it's equivalent to 'f', so you should definitely not use it)
and getline() is not a standard C function. The GNU getline()
function (which is probably the one you're refering to) reads in
lines (i.e. stops after a '\n') which might not be what the OP
wants (he's talking about a 'string' which could contain '\n'
characters, not a 'line').
Regards, Jens
 
J

Joe Wright

scanf() doesn't allocate any memory at all (the 'a' flag is a GNU
extension that isn't in C89 and even has a different meaning in C99
where it's equivalent to 'f', so you should definitely not use it)
and getline() is not a standard C function. The GNU getline()
function (which is probably the one you're refering to) reads in
lines (i.e. stops after a '\n') which might not be what the OP
wants (he's talking about a 'string' which could contain '\n'
characters, not a 'line').
Regards, Jens

You are unlikely to find '\0' in a text file. The C 'string' is an
array of char in memory, the last char of which is '\0'. Its name,
fgets() might suggest otherwise, but it reads lines (not strings)
from a text stream and appends a '\0' to make a string in memory. In
the odd case that the line contains a '\0', it is read in but
otherwise ignored by fgets.
 
C

CBFalconer

zalzon said:
Do I have to use char * string and then malloc memory to it?

But malloc(size) can only gurantee that size is allocated. What
if the string is more than size?

Is there any way to find the size of the string before taking it
in and THEN malloc the needed amount?

Repeat your query in the body of the message. Many systems don't
show the subject during reading.

Take a look at ggets(), which is written purely standard C and
handles just this problem. See:

<http://cbfalconer.home.att.net/download/ggets.zip>
 
P

Paul Hsieh

zalzon said:
Do I have to use char * string and then malloc memory to it?

But malloc(size) can only gurantee that size is allocated. What if the
string is more than size?

Is there any way to find the size of the string before taking it in and
THEN malloc the needed amount?

I'm in a rut, help me please :(

From what you've posted, it looks like you really want string *input*
of an arbitrary size. I have written up a fairly comprehensive
article on the issue, with included source, here:

http://www.azillionmonkeys.com/qed/userInput.html

However, C's whole \0 terminated, and fixed buffer paradigm is
annoying for more issues than just input. I've written a safe,
portable and fast library that lets you substantially ignore C's
limited char buffer nonsense, and treat strings closer to how most
other programming languages do it here:

http://bstring.sf.net/

This library also includes very simplistic input functions for
arbitrary input streams (not necessarily just files). Most
importantly it is interoperable with C's char * paradigm (so if you
are forced to use C semantics, you aren't screwed) and comes with a
complement of functions that totally supersets the C's string handling
functions.
 
S

Smoker

Do I have to use char * string and then malloc memory to it?

But malloc(size) can only gurantee that size is allocated. What if the
string is more than size?

Is there any way to find the size of the string before taking it in and
THEN malloc the needed amount?

I'm in a rut, help me please :(


How does the code you thought about in the original post "take in a
string of any size"?

1) a file pointer
look at the discussion above

2) Call to a function that should pass in the string of unknown size

Do not allocate at all. That function should know more about the
string, so pass in a pointer to your char pointer and let that
function do the allocation and copying stuff:

int any_string (char **);
[...]
char * str;

if( !any_string( &str )) {

// We got the string
} else {
// some error ocurred
}

You may want to pass around the length of the string as well:
int any_string (char ** str_ptr, unsigned long str_length);



I think there is no global answer to your question, the way you handle
it depends on the way you "take it in".
 

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,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top