By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,935 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,935 IT Pros & Developers. It's quick & easy.

How to determine number of lines in char **

P: n/a
Dear all,

As of yesterday I have this function:

char ** lines = read_file("filename.txt");
Now, I want to print the contents of lines. A first attempt:

int i=0;
while (lines[i]) {
printf("%s\n",lines[i]);
i++;
}

Seg faults after printing all lines because it tries printing beyond
the end of the array.

How can I determine how many variable-length lines are in the array?

The only way I can think of is to modify readfile so it can be called
as:

int numlines=read_file("filename.txt", lines);
Is there another/better way?

Thanks for any tips,

Pakt.

Aug 1 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
pa**********@gmail.com wrote:
Dear all,

As of yesterday I have this function:

char ** lines = read_file("filename.txt");
Now, I want to print the contents of lines. A first attempt:

int i=0;
while (lines[i]) {
printf("%s\n",lines[i]);
i++;
}

Seg faults after printing all lines because it tries printing beyond
the end of the array.

How can I determine how many variable-length lines are in the array?
Stick a null pointer at the end of the array of lines.

--
Ian Collins.
Aug 1 '07 #2

P: n/a
On Aug 1, 6:05 pm, Ian Collins <ian-n...@hotmail.comwrote:
paktsardi...@gmail.com wrote:
Dear all,
As of yesterday I have this function:
char ** lines = read_file("filename.txt");
Now, I want to print the contents of lines. A first attempt:
int i=0;
while (lines[i]) {
printf("%s\n",lines[i]);
i++;
}
Seg faults after printing all lines because it tries printing beyond
the end of the array.
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.

--
Ian Collins.
Brilliant! Thanks again!

Aug 1 '07 #3

P: n/a
Ian Collins wrote:
pa**********@gmail.com wrote:
.... snip ...
>>
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.
This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Aug 2 '07 #4

P: n/a
CBFalconer wrote:
Ian Collins wrote:
>pa**********@gmail.com wrote:
.... snip ...
>>How can I determine how many variable-length lines are in the array?
Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.
Which in this context would be a pointer to a '\0'.

--
Ian Collins.
Aug 2 '07 #5

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Ian Collins wrote:
>pa**********@gmail.com wrote:
... snip ...
>>>
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.
Of course. Why would you even consider representing an empty line as
a null pointer?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 2 '07 #6

P: n/a
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Ian Collins wrote:
>>pa**********@gmail.com wrote:
... snip ...
>>>>
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.

Of course. Why would you even consider representing an empty line as
a null pointer?
Because it requires no new storage space.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 2 '07 #7

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>Ian Collins wrote:
pa**********@gmail.com wrote:

... snip ...
>
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.

Of course. Why would you even consider representing an empty line as
a null pointer?

Because it requires no new storage space.
Neither, in effect, does an empty string. Of course, that would
unnecessarily complicate the usage of the array of strings, but that also
happens when it contains a mixture of strings and nulls.

--
Ben.
Aug 2 '07 #8

P: n/a
Ben Bacarisse wrote:
CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
>>CBFalconer <cb********@yahoo.comwrites:
Ian Collins wrote:
pa**********@gmail.com wrote:
>
... snip ...
>>
>How can I determine how many variable-length lines are in the
>array?
>
Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other
than a NULL pointer. It may be an empty string.

Of course. Why would you even consider representing an empty
line as a null pointer?

Because it requires no new storage space.

Neither, in effect, does an empty string. Of course, that would
unnecessarily complicate the usage of the array of strings, but
that also happens when it contains a mixture of strings and nulls.
Yes it does, and it may be relatively major. When assigned via
malloc, the space needs to be suitably aligned for all types. This
often means assignment in blocks of 8 or 16 bytes. Thus that one
byte terminator really eats up 16, plus the overhead involved in
keeping track of the storage, etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 2 '07 #9

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Ben Bacarisse wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
Ian Collins wrote:
>pa**********@gmail.com wrote:
>>
... snip ...
>>>
>>How can I determine how many variable-length lines are in the
>>array?
>>
>Stick a null pointer at the end of the array of lines.
>
This requires that an empty line be defined as something other
than a NULL pointer. It may be an empty string.

Of course. Why would you even consider representing an empty
line as a null pointer?

Because it requires no new storage space.

Neither, in effect, does an empty string. Of course, that would
unnecessarily complicate the usage of the array of strings, but
that also happens when it contains a mixture of strings and nulls.

Yes it does, and it may be relatively major. When assigned via
malloc, the space needs to be suitably aligned for all types. This
often means assignment in blocks of 8 or 16 bytes. Thus that one
byte terminator really eats up 16, plus the overhead involved in
keeping track of the storage, etc.
That is not what I had in mind. It is possible to indicate empty
lines by using a pointer to the same, statically allocated, string.

I am not advocating doing this in anything but the oddest of
situations (space being very tight and the usage of the pointers being
such that a shared empty string is more convenient than repeatedly
checking for NULL).

The trouble with using NULL, as you know, is that is complicates every
use of very line. At least with a shared empty string all the lines
*are* strings.

--
Ben.
Aug 2 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.