Help With Arrays

C

Craig

Hello friends at comp.lang.c,

I'm reading from a socket descriptor and copying the data to a buffer using:

char buffer[MAXVALUE];
..
..
nread = read(newSd, line, MAXVALUE);

The string to be copied to the buffer is of varying size, so what I'd like
to know is if there's a way to copy the data, without having to specify a
max value, making the size of the string the same size as the array.

Thanks,
Craig
 
M

Malcolm

Craig said:
Hello friends at comp.lang.c,

I'm reading from a socket descriptor and copying the data to a buffer
using:

char buffer[MAXVALUE];
.
.
nread = read(newSd, line, MAXVALUE);

The string to be copied to the buffer is of varying size, so what I'd like
to know is if there's a way to copy the data, without having to specify a
max value, making the size of the string the same size as the array.
(IFYM size of the array same as size of the string)
As you probably know, a C string is a NUL-terminarted list of characters.
In C, an array must have a size known at runtime. If the string is smaller
than the array, you have no problem (the NUL gives the length). if it is
larger, you will overflow.

The way round this is to use malloc(). When you know the size of the string,
call malloc() with the length plus one for the NUL. This means that "buffer"
must be a pointer rather than an array. You cna also use realloc() to grow
and shrink the buffer as required.
 
E

Emmanuel Delahaye

Craig wrote on 30/04/05 :
Hello friends at comp.lang.c,

I'm reading from a socket descriptor and copying the data to a buffer using:

char buffer[MAXVALUE];
.
nread = read(newSd, line, MAXVALUE);

The string to be copied to the buffer is of varying size, so what I'd like
to know is if there's a way to copy the data, without having to specify a
max value, making the size of the string the same size as the array.

First of all, read() is not part of the C standard (but it's part of
POSIX.1 which is an 'Open Group' standard)

That said, you want this;

char buffer[SIZE];
int nread = read (fd, buffer, sizeof buffer);

The actual number of read bytes is in nread. The given size is just an
indication of the maximum number of byte to be read in one gulp... Can
be less, can't be more.

If you intend to receive a string, I suggest the following :

char buffer[SIZE];
int nread = read (fd, buffer, sizeof buffer - 1);

if (nread != -1)
{
buffer[nread] = 0;

/* [1] */
}

[1] From this sequence point, you have a well-formed string in
'buffer', but there is no guarantee that this string is a well-formed
line (I mean a sequence of characters terminated by a '\n' and followed
by a 0)

--
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"
 
L

Lawrence Kirby

Craig wrote on 30/04/05 :
Hello friends at comp.lang.c,

I'm reading from a socket descriptor and copying the data to a buffer using:

char buffer[MAXVALUE];
.
nread = read(newSd, line, MAXVALUE);

The string to be copied to the buffer is of varying size, so what I'd like
to know is if there's a way to copy the data, without having to specify a
max value, making the size of the string the same size as the array.

First of all, read() is not part of the C standard (but it's part of
POSIX.1 which is an 'Open Group' standard)

That said, you want this;

char buffer[SIZE];
int nread = read (fd, buffer, sizeof buffer);

IMHO it is better to write SIZE here rather than sizeof buffer. This
will continue to work if, for example, buffer is made a pointer to
allocated memory.

Lawrence
 
G

Gregory Pietsch

Well, let's see, another job for (cue theme music) ...
dynamically-allocated strings!

Get FreeDOS edlin and notice how it reads characters from files one at
a time, appending them to the end of a dynamically-allocated string.
Even though it reads from a file, not a socket (sockets aren't
mentioned in the C standard), the principle is the same.

Couldn't resist, but dynamically-allocated strings solve a lot of
problems.

Gregory Pietsch
 

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,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top