Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > knowing exact string array length ?

Reply
Thread Tools

knowing exact string array length ?

 
 
alberto
Guest
Posts: n/a
 
      06-17-2006
Hi. Im newbie in C language. I have a binary file with many character
arrays of 50 character defined as

char array[50]

But in some cases, many of these 50 characters are not being used. I
would like to know how could I know how many characters are really being
used in each array ?

Thanks
Alberto
 
Reply With Quote
 
 
 
 
Bill Pursell
Guest
Posts: n/a
 
      06-17-2006

alberto wrote:
> Hi. Im newbie in C language. I have a binary file with many character
> arrays of 50 character defined as
>
> char array[50]
>
> But in some cases, many of these 50 characters are not being used. I
> would like to know how could I know how many characters are really being
> used in each array ?



What do you mean by "used"? Do you mean that they are non-zero?
The following will give you a count of how many of the first characters
are non-zero.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BLOCK_SIZE 50
int
main(void)
{
char buf[BLOCK_SIZE+1];
int count=0;

buf[BLOCK_SIZE] = 0;
while(fread(buf, sizeof *buf, BLOCK_SIZE, stdin) == BLOCK_SIZE)
printf("Block %d used %d bytes\n", ++count,
strlen(buf));
}

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-17-2006
alberto <> writes:
> Hi. Im newbie in C language. I have a binary file with many character
> arrays of 50 character defined as
>
> char array[50]
>
> But in some cases, many of these 50 characters are not being used. I
> would like to know how could I know how many characters are really
> being used in each array ?


If you can define what you mean by "used", the answer will be obvious.
If you can't, there is no answer.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
alberto
Guest
Posts: n/a
 
      06-17-2006
Bill Pursell escribió:
> alberto wrote:
>> Hi. Im newbie in C language. I have a binary file with many character
>> arrays of 50 character defined as
>>
>> char array[50]
>>
>> But in some cases, many of these 50 characters are not being used. I
>> would like to know how could I know how many characters are really being
>> used in each array ?

>
>
> What do you mean by "used"? Do you mean that they are non-zero?
> The following will give you a count of how many of the first characters
> are non-zero.
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
>
> #define BLOCK_SIZE 50
> int
> main(void)
> {
> char buf[BLOCK_SIZE+1];
> int count=0;
>
> buf[BLOCK_SIZE] = 0;
> while(fread(buf, sizeof *buf, BLOCK_SIZE, stdin) == BLOCK_SIZE)
> printf("Block %d used %d bytes\n", ++count,
> strlen(buf));
> }
>

tnbx for your answer. Yes, I mean that if I have the array

char buf[50]

then some times it will contain 20 characters not '\0' I want to know
the amount of these characters, because I want to do a linked dynamic
list with pointers and each node should have the "string" of the static
array of 50 chars with really characters not '\0', so I must know the
number and after that use calloc or malloc functions

 
Reply With Quote
 
alberto
Guest
Posts: n/a
 
      06-17-2006
Keith Thompson escribió:
> alberto <> writes:
>> Hi. Im newbie in C language. I have a binary file with many character
>> arrays of 50 character defined as
>>
>> char array[50]
>>
>> But in some cases, many of these 50 characters are not being used. I
>> would like to know how could I know how many characters are really
>> being used in each array ?

>
> If you can define what you mean by "used", the answer will be obvious.
> If you can't, there is no answer.
>

I told on previous massage.
For example, I declare this array:

char arr[50];

And I want to put on that variable what user write on the keyboard with
scanf function (for example, his name). But some times the user will
type 20 characters, and other times will type 40 characters...

The same if the array would be on a file. But how do I know exactly how
many characters typed the user ?
 
Reply With Quote
 
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a
 
      06-17-2006
alberto wrote:
> Keith Thompson escribió:

-snip-

>> If you can define what you mean by "used", the answer will be obvious.
>> If you can't, there is no answer.
>>

> I told on previous massage.
> For example, I declare this array:
>
> char arr[50];
>
> And I want to put on that variable what user write on the keyboard with
> scanf function (for example, his name). But some times the user will
> type 20 characters, and other times will type 40 characters...
>
> The same if the array would be on a file. But how do I know exactly how
> many characters typed the user ?


I think you should look at strlen from <string.h>... Isn't that what you
meant by "used"?


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-17-2006
alberto schrieb:
> Keith Thompson escribió:
>> alberto <> writes:
>>
>>> Hi. Im newbie in C language. I have a binary file with many character
>>> arrays of 50 character defined as
>>>
>>> char array[50]
>>>
>>> But in some cases, many of these 50 characters are not being used. I
>>> would like to know how could I know how many characters are really
>>> being used in each array ?

>>
>>
>> If you can define what you mean by "used", the answer will be obvious.
>> If you can't, there is no answer.
>>

> I told on previous massage.
> For example, I declare this array:
>
> char arr[50];
>
> And I want to put on that variable what user write on the keyboard with
> scanf function (for example, his name). But some times the user will
> type 20 characters, and other times will type 40 characters...
>
> The same if the array would be on a file. But how do I know exactly how
> many characters typed the user ?


You restricted yourself to a binary file, thus you do not know
how many characters are "used" -- you read 50 and then determine
how many are "used".
If you change your notion to "the file contains zero-terminated
character sequences (vulgo: strings) each of which is no longer
than 50 characters including the terminator", you can read up
to 50 characters, stop earlier when you encounter '\0' and know
how many characters you read ("used") up to that point.
This is curiously close to "the text file contains lines of up
to 50 characters including the newline character". C provides
a function to deal with this case: fgets(). You can use fgets()
also to read from stdin. If you want to be on the safe side,
you can check whether the user really entered/the file really
contained such a character sequence: The last character of the
string's "content" must be a '\n'.
Especially for reading from stdin, you often are restricted to
line-buffered input, so you get the user's input only after
the user hit return.
strlen() can be used to determine the number of characters plus
the '\n'.
If you do not need the '\n', you can overwrite it with '\0'.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
alberto
Guest
Posts: n/a
 
      06-17-2006
Michael Mair escribió:
> alberto schrieb:
>> Keith Thompson escribió:
>>> alberto <> writes:
>>>
>>>> Hi. Im newbie in C language. I have a binary file with many character
>>>> arrays of 50 character defined as
>>>>
>>>> char array[50]
>>>>
>>>> But in some cases, many of these 50 characters are not being used. I
>>>> would like to know how could I know how many characters are really
>>>> being used in each array ?
>>>
>>>
>>> If you can define what you mean by "used", the answer will be obvious.
>>> If you can't, there is no answer.
>>>

>> I told on previous massage.
>> For example, I declare this array:
>>
>> char arr[50];
>>
>> And I want to put on that variable what user write on the keyboard
>> with scanf function (for example, his name). But some times the user
>> will type 20 characters, and other times will type 40 characters...
>>
>> The same if the array would be on a file. But how do I know exactly
>> how many characters typed the user ?

>
> You restricted yourself to a binary file, thus you do not know
> how many characters are "used" -- you read 50 and then determine
> how many are "used".
> If you change your notion to "the file contains zero-terminated
> character sequences (vulgo: strings) each of which is no longer
> than 50 characters including the terminator", you can read up
> to 50 characters, stop earlier when you encounter '\0' and know
> how many characters you read ("used") up to that point.
> This is curiously close to "the text file contains lines of up
> to 50 characters including the newline character". C provides
> a function to deal with this case: fgets(). You can use fgets()
> also to read from stdin. If you want to be on the safe side,
> you can check whether the user really entered/the file really
> contained such a character sequence: The last character of the
> string's "content" must be a '\n'.
> Especially for reading from stdin, you often are restricted to
> line-buffered input, so you get the user's input only after
> the user hit return.
> strlen() can be used to determine the number of characters plus
> the '\n'.
> If you do not need the '\n', you can overwrite it with '\0'.
>
>
> Cheers
> Michael

tnx for info. Yes, the array of chars is a member of a struct and stored
in a binery file. I think I should read each "register" of the file, and
then try to determine the real lenght of the field "array of char" and
after that call calloc or malloc function properly. Correct ?
Tnx
Alberto
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-17-2006



"alberto" <> wrote in message
> tnx for info. Yes, the array of chars is a member of a struct and stored
> in a binery file. I think I should read each "register" of the file, and
> then try to determine the real lenght of the field "array of char" and
> after that call calloc or malloc function properly. Correct ?
> Tnx
> Alberto


That's right.

struct record
{
char *astring;
};

Will store a string of any length, if you allocate the "astring" member with
malloc().
It is very debatable whether there will be any benefit over a fixed buffer
size of 50 bytes. Normally malloc uses a few bytes for overhead, and then
you have the extra run-time and complication of calling the allocation
function and freeing it.
However if the strings were several kilobytes in length, this would be the
only realistic option.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-17-2006
Malcolm said:

<snip>

> struct record
> {
> char *astring;
> };
>
> Will store a string of any length, if you allocate the "astring" member
> with malloc().


Nonsense, Malcolm. Care to try again?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reserving binary Buffer for struct.pack_into() of exact length Hans Müller Python 1 03-19-2010 08:33 PM
String exceeding length - Getting absolute string length james.w.appleby@gmail.com Java 5 01-11-2007 12:07 AM
atof approximates the String, I need exact value of the string passed oksuresh@gmail.com C++ 21 04-13-2006 11:08 AM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57