473,383 Members | 1,813 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,383 software developers and data experts.

knowing exact string array length ?

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
26 2521

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
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
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
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
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
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
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

"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
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
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
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
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
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
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
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
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
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

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

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
4
by: Jason | last post by:
I need to open a file for reading, but I only know part of it's name. The file I want to read is in the format of xxx-yyyy-zzz.EXT The last three digits of the file name are different on each...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.