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 26 2586
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));
}
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_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.
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
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 ?
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
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.
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
"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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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...
|
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...?
|
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);
|
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
| |
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
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |