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

knowing exact string array length ?

P: n/a
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
Jun 17 '06 #1
Share this Question
Share on Google+
26 Replies


P: n/a

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));
}

Jun 17 '06 #2

P: n/a
alberto <a@a.com> 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) 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.
Jun 17 '06 #3

P: n/a
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

Jun 17 '06 #4

P: n/a
Keith Thompson escribiˇ:
alberto <a@a.com> 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 ?
Jun 17 '06 #5

P: n/a
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
Jun 17 '06 #6

P: n/a
alberto schrieb:
Keith Thompson escribiˇ:
alberto <a@a.com> 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.
Jun 17 '06 #7

P: n/a
Michael Mair escribiˇ:
alberto schrieb:
Keith Thompson escribiˇ:
alberto <a@a.com> 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
Jun 17 '06 #8

P: n/a

"alberto" <a@a.com> 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
Jun 17 '06 #9

P: n/a
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)
Jun 17 '06 #10

P: n/a
alberto schrieb:
Michael Mair escribiˇ:
alberto schrieb:
Keith Thompson escribiˇ:
alberto <a@a.com> 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'.


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 ?


This depends. As this is a newsgroup about C, use code to convey
what you are talking about -- this makes clear what you are
trying to do.
If I understood you correctly, you have a binary file which
contains a number of data "registers" which you read in as
struct:
struct myFooRegister {
....
char bar[50];
....
};
Now, you want to "repackage" the data to another internal format,
say
struct myBaz {
....
char *qux;
....
};
You could have gone with "char qux[50];" as well but have
reason to believe that the additional effort of allocating and
freeing memory is well spent as you really need the memory[*].
Now, you want to have
struct myBaz baz;
char *baz_qux = malloc(strlen(foo_reg.bar) + 1);
if (baz_qux != NULL) {
strcpy(baz_qux, foo_reg.bar);
}
else {
/* Your error handling here */
}
baz.qux = baz_qux;
Is this what you mean?

Cheers
Michael
[*] For fifty bytes, it usually is not worth it. Say you have
25 bytes "used" on average. You get at least an additional
sizeof (char*) bytes for the pointer itself, some memory for
the system's internal allocation data, and maybe the system
gives you always 64 bytes, 256 bytes, or 1K to make sure that
there is no "odd-sized hole" in the memory and that you can
realloc() without having to copy the contents.
You _can_ of course write some memory handling of your own
to take care of that but it probably is not worth the overhead.

However, it certainly is a good exercise :-)
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 17 '06 #11

P: n/a
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


Assuming a binary file consisting of 'char array[50]' elements, and
assuming further that the data in the elements are strings, you can
fseek the file on 50-byte boundaries, fread 50 bytes into a memory array
and then run strlen() on the array.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 17 '06 #12

P: n/a
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.
Jun 17 '06 #13

P: n/a
Richard Heathfield <in*****@invalid.invalid> writes:
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?


It makes sense if you assume that "allocate a pointer" is shorthand
for "allocate an array and set the pointer to point to its first
element". (But I'm not sure I see the point of wrapping it in a
structure.)

--
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.
Jun 17 '06 #14

P: n/a
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalid> writes:
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?


It makes sense if you assume that "allocate a pointer" is shorthand
for "allocate an array and set the pointer to point to its first
element".


It still won't store a string of *any* length, though. There are infinitely
many strings. Of these, infinitely many are infinitely long, and Malcolm
could reasonably argue that he didn't mean those, but if we discount them,
there remain infinitely many strings that are finite in length but still so
very very long that they are unrepresentable within a single universe, let
alone a single machine.
(But I'm not sure I see the point of wrapping it in a
structure.)


That, at least, makes sense as a first cut which is heading in a direction
such as this:

struct elastistring
{
char *astring;
size_t curlen;
size_t maxlen;
};

--
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)
Jun 17 '06 #15

P: n/a
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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
Jun 18 '06 #16

P: n/a
On Sat, 17 Jun 2006 10:50:53 +0200, alberto <a@a.com> wrote:
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


Binary files often contain '\0' bytes in positions other than the end
of strings. Is the data in your binary file truly strings or is it
possible that there could be imbedded '\0' bytes and more data in buf
that follows? If they are strings, why are you processing the file in
binary mode?
Remove del for email
Jun 18 '06 #17

P: n/a
Barry Schwarz <sc******@doezl.net> writes:
[...]
Binary files often contain '\0' bytes in positions other than the end
of strings. Is the data in your binary file truly strings or is it
possible that there could be imbedded '\0' bytes and more data in buf
that follows? If they are strings, why are you processing the file in
binary mode?


Strings are arrays of char terminated by '\0'. A '\0' character
normally wouldn't appear in a text file. (Text files contain lines,
which are often read into strings.)

--
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.
Jun 18 '06 #18

P: n/a

"Richard Heathfield" <in*****@invalid.invalid> wrote

It makes sense if you assume that "allocate a pointer" is shorthand
for "allocate an array and set the pointer to point to its first
element".


It still won't store a string of *any* length, though. There are
infinitely
many strings. Of these, infinitely many are infinitely long, and Malcolm
could reasonably argue that he didn't mean those, but if we discount them,
there remain infinitely many strings that are finite in length but still
so
very very long that they are unrepresentable within a single universe, let
alone a single machine.

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.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm

Jun 18 '06 #19

P: n/a
Barry Schwarz escribiˇ:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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
Jun 18 '06 #20

P: n/a
Malcolm said:
"Richard Heathfield" <in*****@invalid.invalid> wrote

It makes sense if you assume that "allocate a pointer" is shorthand
for "allocate an array and set the pointer to point to its first
element".
It still won't store a string of *any* length, though. There
are infinitely many strings. Of these, infinitely many are
infinitely long, and Malcolm could reasonably argue that he
didn't mean those, but if we discount them, there remain
infinitely many strings that are finite in length but still so
very very long that they are unrepresentable within a single
universe, let alone a single machine.

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! :-)

--
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)
Jun 18 '06 #21

P: n/a
"Malcolm" <re*******@btinternet.com> writes:
[...]
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.

--
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.
Jun 18 '06 #22

P: n/a
Barry Schwarz wrote:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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.

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.
Jun 18 '06 #23

P: n/a
johnny wrote:
Barry Schwarz escribiˇ:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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);
Jun 18 '06 #24

P: n/a
On Sun, 18 Jun 2006 08:07:29 +0200, johnny <a@a.com> wrote:
Barry Schwarz escribiˇ:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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
Jun 18 '06 #25

P: n/a
On Sun, 18 Jun 2006 13:00:08 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
Barry Schwarz wrote:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPANnutextonline.com> wrote:
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
Jun 19 '06 #26

P: n/a
Barry Schwarz wrote:
.... 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.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 19 '06 #27

This discussion thread is closed

Replies have been disabled for this discussion.