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

Help With Arrays

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
Nov 14 '05 #1
3 1140

"Craig" <NO****************@highstream.net> wrote
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.
Nov 14 '05 #2
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"

Nov 14 '05 #3
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

Nov 14 '05 #4

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

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.