473,799 Members | 3,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
26 2587
Malcolm said:
"Richard Heathfield" <in*****@invali d.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*******@btin ternet.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_Keit h) 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*****@NOSPAN nutextonline.co m> 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*****@NOSPAN nutextonline.co m> 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.data 3);
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*****@NOSPAN nutextonline.co m> 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*****@NOSPAN nutextonline.co m> wrote:
Barry Schwarz wrote:
On Sat, 17 Jun 2006 12:39:15 -0500, Joe Estock
<je*****@NOSPAN nutextonline.co m> 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
23178
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 generators, so my solution at the moment is: import string import random random.seed(14)
3
2325
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 gotten everything working that I want working so far, except for std::istream& operator>>(std::istream&, Str&) The way my Str class manages itself of course requires that the size of the char array to store is known when it is allocated. The...
7
7606
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 easy way to convert a String to a BitArray (and back again)? I explained the ultimate goal (XORing two Strings) so that, if anyone has a better idea of how to go about this they may (hopefully) bring that up...?
2
2195
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 = "field != value" result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
4
9700
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 writing the JavaScript doesn't have to pass the index in the "onChange" event name. I thought that one might be able to use "this.value" or compare this as
4
2813
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 pc, for example PC7 has xxx-yyyy-zz1.ext PC5 has xxx-yyyy-zz2.ext PC3 has xxx-yyyy-zz8.ext
12
3231
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
10
18638
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
14
1734
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 illogically? Here's a test I just put together that splits on "&". The test strings are: "a&b" = (Correct!) I expect array length 2 and I get 2 "a&" = (Incorrect!) I expect array length 1 but I get 2 "a" = (Correct!) I expect array length 1 and...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.