473,714 Members | 2,793 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 1650
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
2342
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
1736
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
4333
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
1800
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
1569
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
4700
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
2213
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
7894
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
2820
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
8797
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
9309
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9010
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...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6629
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
5945
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
4717
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2107
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.