473,396 Members | 2,059 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,396 software developers and data experts.

How to determine number of lines in char **

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
9 1759
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: joe | last post by:
If I had a webpage that displayed a database in tabular form, it would be nice to know how many lines of text the browser could display without scrolling, then have the cgi script output the...
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
6
by: lisa.lin | last post by:
Hi,all Is there any library function to tell a string to be a number? I did't find it in <string.h>, does anyone kow? Thanks in advance! Lisa Lin
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
6
by: Tom McLaughlin | last post by:
How can I determine the numbers of lines of text that are visible in a textbox? Tom
7
by: TheGanjaMan | last post by:
Hi people, I'm stuck on a problem and I was wondering if there was a way around: I'm trying to find the number of lines in a comma delimited text file. I have a progress bar that should...
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.