knowing exact string array length ?

R

Richard Heathfield

Malcolm said:
A infinite string isn't a string, because C stirngs are NUL-terminated and
an infinite array has no terminating member.

Touche'. (Except that C strings are actually null-terminated, not
NUL-terminated.)
I do take the point about long strings. If a size_t won't hold the length
of my string (the Encyclopedia Britannica, not a contrived example in any
way) the what is the point of it?

The point of the string depends on whether your application needs it or is
required to be able to process it. The point of size_t is to be able to
store information on the size or number of objects, and it can easily do
this without being an arbitrary-precision type.
I recommend for C2006 a arbitrary-precison representation of size_t.

Go for it. Good luck in committee! :)
 
K

Keith Thompson

Malcolm said:
A infinite string isn't a string, because C stirngs are NUL-terminated and
an infinite array has no terminating member.
I do take the point about long strings. If a size_t won't hold the length of
my string (the Encyclopedia Britannica, not a contrived example in any way)
the what is the point of it?

I recommend for C2006 a arbitrary-precison representation of size_t.
[...]

size_t isn't required to be more than 16 bits, but a 32-bit size_t is
more than big enough to index the Encyclopedia Britannica.
 
J

Joe Estock

Barry said:
alberto said:
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
I presume you are wanting to know how many bytes were read from the
file. If this is the case then you can simply store the return value of
fread. This will tell you how many bytes it actually read.

fread is not a string oriented function. In the absence of an I/O
error or an end of data condition, it will always read the requested
number of bytes, regardless of how many are '\0'. Additionally,
unless fread is called with the second argument set to 1, the return
value is the number of objects read, not the number of bytes.


Remove del for email

It doesn't matter if fread is a string oriented function or not. The OP
wasn't looking for a way to read a text file; it was clearly stated that
it was a binary file hence my recommendation for fread which is a binary
safe method of reading data from a file. fread does *not* *always* read
the requested number of bytes. If you request 30 bytes and there are
only 10 left in the file it will return 10 and EOF will be set.

size_t fread(void *BUF, size_t SIZE, size_t COUNT, FILE *FP);

"'fread' attempts to copy, from the file or stream identified by FP,
COUNT elements (each of size SIZE) into memory, starting at BUF.
`fread' may copy fewer elements than COUNT if an error, or end of file,
intervenes."

I'm presuming that the OP knows how to read a manual and search google
for information on the fread function - my intent was to offer a valid
solution for reading data from a binary file.
 
J

Joe Estock

johnny said:
Barry Schwarz 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 ?

Thanks
Alberto
I presume you are wanting to know how many bytes were read from the
file. If this is the case then you can simply store the return value
of fread. This will tell you how many bytes it actually read.

fread is not a string oriented function. In the absence of an I/O
error or an end of data condition, it will always read the requested
number of bytes, regardless of how many are '\0'. Additionally,
unless fread is called with the second argument set to 1, the return
value is the number of objects read, not the number of bytes.


Remove del for email
the file is a binary file containing some "structs" , and one field of
the struct is

char array[50]

which I must "cut" when I find the first '\0' character to know the real
characters used.

So I think fread can be used here

If they are stored in a structure then you can simply define the
structure and read that from the file. For example:

struct foo
{
int data1;
long data2;
char data3[30];
};

struct foo bar;
/* populate struct */
fwrite(&bar, sizeof(struct foo *), 1, filepointer);

/* later on ... */
struct foo bar;
fread(&bar, sizeof(struct foo *), 1, filepointer);

/* should be safe to do here provided that the
* string was null terminated before written to
* the file */
len = strlen(bar.data3);
 
B

Barry Schwarz

Barry Schwarz 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 ?

Thanks
Alberto
I presume you are wanting to know how many bytes were read from the
file. If this is the case then you can simply store the return value of
fread. This will tell you how many bytes it actually read.

fread is not a string oriented function. In the absence of an I/O
error or an end of data condition, it will always read the requested
number of bytes, regardless of how many are '\0'. Additionally,
unless fread is called with the second argument set to 1, the return
value is the number of objects read, not the number of bytes.


Remove del for email
the file is a binary file containing some "structs" , and one field of
the struct is

char array[50]

which I must "cut" when I find the first '\0' character to know the real
characters used.

So I think fread can be used here

This thread was started by someone calling himself alberto using the
same dummy email as you. Are you him? If so why are you changing
names?

For a binary file, I would use fread also.

My comment was directed at Joe's assertion that the return from fread
will tell you the number of bytes in the string. It won't.

Your description is consistent with the contents of array being a
string. If that's the case, strlen() will tell you how many bytes are
in the string up to the first '\0'.

You are reading a struct. Was the struct written to the file using
the same struct declaration, compiler, packing options, and hardware
as the ones you are using to read it? If not, you may have to do
additional work to get the data being read to line up with the members
of the struct in your input program.


Remove del for email
 
B

Barry Schwarz

Barry said:
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 ?

Thanks
Alberto
I presume you are wanting to know how many bytes were read from the
file. If this is the case then you can simply store the return value of
fread. This will tell you how many bytes it actually read.

fread is not a string oriented function. In the absence of an I/O
error or an end of data condition, it will always read the requested
number of bytes, regardless of how many are '\0'. Additionally,
unless fread is called with the second argument set to 1, the return
value is the number of objects read, not the number of bytes.


Remove del for email

It doesn't matter if fread is a string oriented function or not. The OP
wasn't looking for a way to read a text file; it was clearly stated that
it was a binary file hence my recommendation for fread which is a binary
safe method of reading data from a file. fread does *not* *always* read
the requested number of bytes. If you request 30 bytes and there are
only 10 left in the file it will return 10 and EOF will be set.

What part of "in the absence of ... end of data condition" does not
match the situation you describe.

EOF is a macro. It cannot be set.
size_t fread(void *BUF, size_t SIZE, size_t COUNT, FILE *FP);

"'fread' attempts to copy, from the file or stream identified by FP,
COUNT elements (each of size SIZE) into memory, starting at BUF.
`fread' may copy fewer elements than COUNT if an error, or end of file,
intervenes."

I'm presuming that the OP knows how to read a manual and search google
for information on the fread function - my intent was to offer a valid
solution for reading data from a binary file.

As the OP explained in several messages, he doesn't have short
records. He has full records that contain short data with the end of
the data being indicated by a '\0'. The return from fread will not
tell him how much of the record contains data and how much is
irrelevant trailer.


Remove del for email
 
C

CBFalconer

Barry said:
.... snip ...

As the OP explained in several messages, he doesn't have short
records. He has full records that contain short data with the
end of the data being indicated by a '\0'. The return from fread
will not tell him how much of the record contains data and how
much is irrelevant trailer.

In which case strlen will return the size of the 'used' portion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top