473,811 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamicly build a list of strings?

Hi all;

I need to build a list of strings in one function and use the list in
another function.

ie
buildlist(char list[][], FILE **infile);
{
int i;

....
while(fgets(lin e, LINESIZE, *infile)!=NULL)
{
list[i][strlen[line])=line;

/* I assume I will need malloc here some how? */
}
....
}

main(....)
{
FILE *infile, *outfile
char list[][];

buildlist(char list[][], &infile);
....
uselist(list[][], &outfile);
....
}

But this is all I remember... As you can see I need help.
Regards David. E. Goble
http://www.pnc.com.au/~degoble
degoble[AT]pnc.com.au | dgoble[AT]pnc.com.au
Po Box 648 (9 Murray St), Kingscote, Kangaroo Island SA 5223
Nov 14 '05 #1
7 1657
On Thu, 03 Feb 2005 04:02:42 +1030, David. E. Goble
<de*****@gtech. computing> wrote:
Hi all;

I need to build a list of strings in one function and use the list in
another function.

snip much pseudo code that only confuses the issue

Do you know the maximum number of strings you will need to process?
Do you know the maximum length of each string? If so, you could
simply define the array
char list[MAX_NUM][MAX_LEN];
in main and pass it to both functions.

If some of these values are only known at execution time, then you
will need to malloc and friends. One common technique is to define a
char ** (call it list). Assign this pointer the address of a
dynamically allocated block capable of holding N char*. As you
process each string, allocate space for it and store the address of
that space in list[i]. When i gets up to N, use realloc to expand the
amount of space list points to. Repeat until all strings are
processed.
<<Remove the del for email>>
Nov 14 '05 #2


"David. E. Goble" wrote:
Hi all;

I need to build a list of strings in one function and use the list in
another function.

ie
buildlist(char list[][], FILE **infile);
{
int i;

...
while(fgets(lin e, LINESIZE, *infile)!=NULL)
{
list[i][strlen[line])=line;

/* I assume I will need malloc here some how? */
}
...
}

main(....)
{
FILE *infile, *outfile
char list[][];

buildlist(char list[][], &infile);
...
uselist(list[][], &outfile);
...
}

But this is all I remember... As you can see I need help.
Regards David. E. Goble
http://www.pnc.com.au/~degoble
degoble[AT]pnc.com.au | dgoble[AT]pnc.com.au
Po Box 648 (9 Murray St), Kingscote, Kangaroo Island SA 5223


Not list[i][strlen[line])=line; use strncpy()

if the number and length of the strings is known use a 2D array
If the number of messages is not known you may want to use a linked list.

If you you know the number of messages use an array of pointers.
You can then malloc them one by one as then come in (+1 for the Null)

for extra credit you can read the file, count the strings, then malloc
the array of pointers.

Since there are many ways to do this this is just my opinion.


Nov 14 '05 #3
Neil Kurzman wrote:

<snip>

Not list[i][strlen[line])=line; use strncpy()


<snip>

I would suggest using strlcpy instead. The latest source code is
available from the OpenBSD ftp. strlcpy guarantees null termination
of the string and is more efficient since it does not pad
the string buffer with redundant NULs. strncpy can be a problem if
used improperly (you have to remember to pass the correct
count of characters and then manually terminate the string).
Also, strlcpy is more secure and more intuitive to use.
You simply pass the size of the buffer in bytes as the last
parameter.

/* xstrlcpy.c */
#include <stddef.h>

/*
@synopsis
#include <xstring.h>
size_t xstrlcpy (char *dst, const char *src, size_t siz);
@description
Copies not more than siz-1 characters into the array
pointed to by dst from the string pointed to by src
(characters that follow a null character are not copied).
If copying takes place between two objects that overlap,
the behavior is undefined.

This function does not add redundant NUL characters
to fill in the size requirements. The array pointed
to by dst is guaranteed to be terminated by a NUL
character (unless siz is zero).
@param dst pointer to the destination buffer array
@param src pointer to the source string
@param siz the size of the destination buffer array
object including the space for a terminating
NUL character.
@return
The length of the string pointed to by dst after
copying (the number of characters before the terminating
null character).

If the returned value is greater than or equal to
siz, truncation has occurred.
@remarks
This function should be used in place of the functions
xstrcpy and xstrncpy whenever possible due to the following
reasons:
* reduces the risk of a buffer overflow
* guarantees NUL-termination of the destination buffer
* is more logical since you only have to remember to
pass the size of the entire destination array object
* efficiency is much better than xstrncpy while negligibly
lesser than xstrcpy.

The xstrncpy function is useful when you do not
need to terminate the destination array with a NUL-character
and want to copy exactly the number of characters specified.
@references
* ISO/IEC 9899:1999 (C Standard) -- 7.21.2.4 -- The strncpy function
* "strlcpy and strlcat -- consistent, safe, string copy
and concatenation" - Todd C. Miller and Theo de Raadt
@source
The source code for this function was obtained from the
OpenBSD libc string routines library. It is available under a
BSD-style license.
@see
xstrncpy
xstrcpy
*/
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
xstrlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
register size_t n = siz;

/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}

/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}

return(s - src - 1); /* count does not include NUL */
}

Regards,
Jonathan.

--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"We must do something. This is something. Therefore, we must do this."
- Keith Thompson
Nov 14 '05 #4
David. E. Goble wrote:
Hi all;

I need to build a list of strings in one function and use the list in
another function.

ie
buildlist(char list[][], FILE **infile);
{
int i;

...
while(fgets(lin e, LINESIZE, *infile)!=NULL)
{
list[i][strlen[line])=line;
strlen is a function. Correct this line.
Use strlcpy. See my reply elsewhere.

/* I assume I will need malloc here some how? */
}
...
}

main(....)
{
FILE *infile, *outfile
char list[][];

buildlist(char list[][], &infile);
...
uselist(list[][], &outfile);
...
}


Regards,
Jonathan.

--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"We must do something. This is something. Therefore, we must do this."
- Keith Thompson
Nov 14 '05 #5
On Thu, 03 Feb 2005 04:02:42 +1030, David. E. Goble
<de*****@gtech. computing> wrote:
Hi all;

I need to build a list of strings in one function and use the list in
another function.

snip much pseudo code that only confuses the issue

Do you know the maximum number of strings you will need to process?
Do you know the maximum length of each string? If so, you could
simply define the array
char list[MAX_NUM][MAX_LEN];
in main and pass it to both functions.

If some of these values are only known at execution time, then you
will need to malloc and friends. One common technique is to define a
char ** (call it list). Assign this pointer the address of a
dynamically allocated block capable of holding N char*. As you
process each string, allocate space for it and store the address of
that space in list[i]. When i gets up to N, use realloc to expand the
amount of space list points to. Repeat until all strings are
processed.
<<Remove the del for email>>

<<Remove the del for email>>
Nov 14 '05 #6
Jonathan Burd wrote:
Neil Kurzman wrote:

<snip>

I would suggest using strlcpy instead. The latest source code is
available from the OpenBSD ftp. strlcpy guarantees null termination
of the string and is more efficient since it does not pad
the string buffer with redundant NULs. strncpy can be a problem if
used improperly (you have to remember to pass the correct
count of characters and then manually terminate the string).
Also, strlcpy is more secure and more intuitive to use.
You simply pass the size of the buffer in bytes as the last
parameter.


.... snip code ...

I agree. You can also get a slightly different version at:

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

which accepts NULL as source (but not dest) parameter, and treats
it as an empty string. It also has (to my mind) the consistent
approach to size less than strlen(dest) in strlcat of always
returning the size required for the combined strings. The zip also
contains links to the BSD publications. My code has been put in
the public domain, so there are no restrictions whatsoever on it.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7
"David. E. Goble" <de*****@gtech. computing> wrote in message
news:1s******** *************** *********@4ax.c om...
Hi all;

I need to build a list of strings in one function and use the list in
another function.


The solution should be somewhere in comp.lang.c.hom ework

--
Mabden
Nov 14 '05 #8

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

Similar topics

10
2357
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the ListBox is edited I came up with this code.) This code works but of course it gives me build...
9
1740
by: Jonathan Wilson | last post by:
Basicly, I need to store a string plus a data structure for each entry in this array. It needs to be able to get bigger (but I wont be deleting from it). Also, I need to be able to store the elements in this array somehow.
4
4337
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
1
1801
by: Henke | last post by:
Hi I have a aspx-page with a panel-control. On this panel control I add user controls dynamicly with LoadControl and panel.Controls.Add(myControl). On some of the dynamicly added user controls I have button's and handlers for the click event. Why doesn't the event get fired in the dynamicly added control? Thanks!
7
1574
by: Tim | last post by:
I am trying to load both server and user controls into placeholder controls on a aspx template page at runtime. These values would be strings that are returned from a database query. I know I can do this for user controls easily using: oContent = (ControlBase) Page.LoadControl(Session + ".ascx"); this.plcContent.Controls.Add(oContent); but can not figure out how to do it for server controls. since I do not know what the control...
7
4704
by: Mat | last post by:
I would like to build multilanguage applications -user may have options, in runtime to change the language(switch). -i may need to update languages (grammar..) without rebuild to applications. Or how to write the application in order that user can download language pack as plugin for application Thanks you TIP
10
2235
by: Larry Hastings | last post by:
I'm an indie shareware Windows game developer. In indie shareware game development, download size is terribly important; conventional wisdom holds that--even today--your download should be 5MB or less. I'd like to use Python in my games. However, python24.dll is 1.86MB, and zips down to 877k. I can't afford to devote 1/6 of my download to just the scripting interpreter; I've got music, and textures, and my own crappy code to ship. ...
9
7913
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
10
2828
by: Mo | last post by:
Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasource and must substitute the values in the exact position insuring that paramteres end up...
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.