niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Adaptive datatype (or so...)
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
Author Message
Whatever5k@web.de
*nix forums beginner


Joined: 20 Jul 2006
Posts: 4

PostPosted: Thu Jul 20, 2006 4:46 pm    Post subject: Adaptive datatype (or so...) Reply with quote

Ok, I got a big problem. What I want to do is basically read from a
file. This file contains "symbols". The user specifies how much bytes
one symbols need. For example, in a a text file each symbol would need
one byte. So I want to read into an array, say buf[], were buf[0]
contains the first symbol, b[1] the second, and so on.
How do I realize that? I mean, the program has to be able to handle any
number of bytes per symbol. For example:

file content = "123456789"

example #1. bytes per symbol = 1.
In this case,
buf[0] = 1
buf[1] = 2
buf[2] = 3
etc.

example #2, bytes per symbol = 2.
In this case,
buf[0] = 12
buf[1] = 34
etc.

Any idea how to do this, guys?
Back to top
Eric Sosman
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1246

PostPosted: Thu Jul 20, 2006 5:06 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Whatever5k@web.de wrote On 07/20/06 12:46,:
Quote:
Ok, I got a big problem. What I want to do is basically read from a
file. This file contains "symbols". The user specifies how much bytes
one symbols need. For example, in a a text file each symbol would need
one byte. So I want to read into an array, say buf[], were buf[0]
contains the first symbol, b[1] the second, and so on.
How do I realize that? I mean, the program has to be able to handle any
number of bytes per symbol. For example:

file content = "123456789"

example #1. bytes per symbol = 1.
In this case,
buf[0] = 1
buf[1] = 2
buf[2] = 3
etc.

example #2, bytes per symbol = 2.
In this case,
buf[0] = 12
buf[1] = 34
etc.

Any idea how to do this, guys?

Several, but I don't know how to choose among them.
What do you want to do with these "symbols" after they
have been loaded into the array?

--
Eric.Sosman@sun.com
Back to top
Whatever5k@web.de
*nix forums beginner


Joined: 20 Jul 2006
Posts: 4

PostPosted: Thu Jul 20, 2006 6:50 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Well, I want to insert them into a binary tree. But that's not the
problem.
My problem is to have an array that contains the symbols.

Could you just choose one simple way of doing this job?
Back to top
Default User
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1159

PostPosted: Thu Jul 20, 2006 7:19 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Whatever5k@web.de wrote:

Quote:
Well, I want to insert them into a binary tree. But that's not the
problem.
My problem is to have an array that contains the symbols.

Could you just choose one simple way of doing this job?


Please quote enough of the previous message for context. See how
everybody else in the group does it.


You haven't explained what you are trying to accomplish. What are these
"symbols"? How will they be used once you create the array?





Brian
Back to top
Eric Sosman
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1246

PostPosted: Thu Jul 20, 2006 7:45 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Whatever5k@web.de wrote On 07/20/06 14:50,:
Quote:
Well, I want to insert them into a binary tree. But that's not the
problem.
My problem is to have an array that contains the symbols.

Please quote enough context so your message can stand on
its own. Message propagation on Usenet is both asynchronous
and uncoordinated, meaning that messages do not arrive at all
news servers at the same time or in the same order. It is
entirely possible for a reply to reach a server before the
message it replies to.

Quote:
Could you just choose one simple way of doing this job?

(For those just joining: "this job" is to extract some
kind of "symbols" from a file and store them in an array.
We are told that the symbols are sometimes one character
long and sometimes two, and possibly other lengths, but
that the symbol length is fixed during any given program
execution. We are not told whether these symbols can
just be thought of as strings or are something else; we are
not told whether newlines in the file have any importance
or are just parts of symbols; we are not told very much at
all. I asked what Whatever5k wanted to do with the symbols,
because data structures exist to support the operations to
be performed on the data; without knowing what the operations
are, it is impossible to make an intelligent recommendation.
His response was as you see above, so ...)

Here's one simple way: Allocate a big array of characters
and read the entire file into it. The array will then contain
all the "symbols" from the file.

--
Eric.Sosman@sun.com
Back to top
bill
*nix forums Guru Wannabe


Joined: 12 May 2005
Posts: 241

PostPosted: Thu Jul 20, 2006 8:37 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Whatever5k@web.de wrote:
Quote:
Ok, I got a big problem. What I want to do is basically read from a
file. This file contains "symbols". The user specifies how much bytes
one symbols need. For example, in a a text file each symbol would need
one byte. So I want to read into an array, say buf[], were buf[0]
contains the first symbol, b[1] the second, and so on.
How do I realize that? I mean, the program has to be able to handle any
number of bytes per symbol. For example:

It's not exactly clear what you want, but here's one
idea.

#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 8

void die(char *a)
{
fprintf(stderr, "%s\n", a);
exit(EXIT_FAILURE);
}


void * xmalloc(size_t size)
{
void *ret;
if ( (ret = malloc(size)) == NULL)
die("out of memory");
return ret;
}

int
main(int argc, char **argv)
{
int symbol_size;
char **symbol_array;
size_t symbol_count;
size_t array_size;
char **next_symbol;

symbol_size = (argc > 1) ? atoi(argv[1]) : 1;
if (symbol_size < 0 || symbol_size > MAX_LENGTH)
die("Invalid size");

symbol_array = xmalloc(array_size = BUFSIZ);
next_symbol = symbol_array;
*next_symbol = xmalloc(symbol_size);
symbol_count = 0;
while( fread(*next_symbol, symbol_size, 1, stdin) == 1) {
/*
* Need to check and realloc symbol_array
* if necessary! Left as an exercise.
*/
next_symbol++;
*next_symbol = xmalloc(symbol_size);
symbol_count++;
}
return EXIT_SUCCESS;
}
~
Back to top
Barry Schwarz
*nix forums Guru


Joined: 09 Apr 2005
Posts: 390

PostPosted: Fri Jul 21, 2006 12:51 am    Post subject: Re: Adaptive datatype (or so...) Reply with quote

On 20 Jul 2006 09:46:00 -0700, Whatever5k@web.de wrote:

Quote:
Ok, I got a big problem. What I want to do is basically read from a
file. This file contains "symbols". The user specifies how much bytes
one symbols need. For example, in a a text file each symbol would need
one byte. So I want to read into an array, say buf[], were buf[0]
contains the first symbol, b[1] the second, and so on.
How do I realize that? I mean, the program has to be able to handle any
number of bytes per symbol. For example:

file content = "123456789"

example #1. bytes per symbol = 1.
In this case,
buf[0] = 1
buf[1] = 2
buf[2] = 3
etc.

example #2, bytes per symbol = 2.
In this case,
buf[0] = 12
buf[1] = 34
etc.

Any idea how to do this, guys?

I recommend a dynamic array of pointers to strings. Once you decide
on the number of bytes per symbol (bps), something like the following
will work (error checking of malloc omitted for brevity):

char **ptr;
int i = 0;
ptr = malloc(n * sizeof *ptr); /*for some initial quantity of
strings*/
while (/*more strings to process*/) {
ptr[i] = malloc(bps+1);
strncpy(ptr[i], /*pointer to starting byte for next
symbol*/, bps);
ptr[i++][bps] = '\0';
}

You will need to include a check for i exceeding the number of
pointers ptr points to. When it does, realloc ptr to point to a
larger number and continue.

If your data is binary rather than text, you can do the same thing
with arrays of unsigned char and use memcpy instead of strncpy. The
extra space for the terminating '\0' would not be needed.



Remove del for email
Back to top
Whatever5k@web.de
*nix forums beginner


Joined: 20 Jul 2006
Posts: 4

PostPosted: Fri Jul 21, 2006 5:23 am    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Thank you for all those replies.
OK, so I have a file, this can be binary or text or anything. What I
want to do is read from that file, symbol by symbol. What I mean by
symbol is just a certain amount of bytes. For example, I want to be
able to read from the size with a symbol size of 2 bytes. This would
mean that at the end I would have an array and each entry would contain
2 bytes of information from the file.
Oh and the file can also be binary. Is it more clear now? What I want
to do with the symbols later on is just count them. I want to see how
many different symbols are in the file.

Thanks.
Back to top
Whatever5k@web.de
*nix forums beginner


Joined: 20 Jul 2006
Posts: 4

PostPosted: Fri Jul 21, 2006 10:11 am    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Barry, your example would not work for a binary file.
OK, here is another example. Let's say I have got a binary file, that
contains the year and month number of today. Now, this would be written
with 0 and 1, but it would look like this: 200607. Ok, we would say
that one symbol occupies 4 bytes. So 2006 would be the first symbol and
07 the second. What I want to have is an array so that ptr[0] = 2006
and ptr[1] = 07.
I don't think that would work with your examples, would it?
Back to top
Eric Sosman
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1246

PostPosted: Fri Jul 21, 2006 12:28 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

Whatever5k@web.de wrote:
Quote:
Barry, your example would not work for a binary file.
OK, here is another example. Let's say I have got a binary file, that
contains the year and month number of today. Now, this would be written
with 0 and 1, but it would look like this: 200607.

Your description of the data format is still unclear
(to me, anyhow). Do you mean that the file contains the
number "two hundred thousand six hundred seven" as a
binary integer in the machine's native form (probably four
or eight bytes long)? What does "look like this" mean?

Quote:
Ok, we would say
that one symbol occupies 4 bytes. So 2006 would be the first symbol and
07 the second.

It sounds like 07xx would be the second, where the x's
are two more bytes. What do you mean when you say 07 is
a four-byte "symbol?"

Quote:
What I want to have is an array so that ptr[0] = 2006
and ptr[1] = 07.

It seems you don't realize that C supports many different
data types, and can represent 2006 in many different ways.
Some of them are

- As an int. The number two thousand six would look like
...011111010110 in the machine, where the "..." stand
for a machine-dependent number of leading zero bits.

- As another integer type: signed or unsigned long long,
long, int, short, and so on. The value would be as above,
but perhaps with more or fewer leading zeroes.

- As a float. C doesn't prescribe any particular floating-
point format, but on many machines two thousand six would
be represented as {0.9794921875 times two to the twelfth}
and might look like 01000100111110101100000000000000 if
viewed as a sequence of bits.

- As another floating-point type: double or long double.
Again, C doesn't prescribe the exact format, but it is
likely to be somewhat like that shown for float.

- As a string of four digits followed by a fifth all-zero
byte (to mark the end of the string). On many machines
this would look like 00110010 00110000 00110000 00110110
00000000 in five consecutive memory locations.

- As a pointer to a string of the form described above.
Strings are really arrays, and C cannot manipulate arrays
as freely as it handles other kinds of objects, so it is
often desirable to store the strings "elsewhere," leave
them pretty much alone, and work with pointers to them
instead. (Especially given the confusion over the "four-
byte symbol" 07 -- if the symbols actually have different
lengths, it will be cumbersome to work with them directly
as arrays.)

Let me repeat: These are only *some* of the ways you might
represent a "symbol" in a C program. Also, these are variations
on ways to represent just *one* of your "symbols;" there are
additional decisions to be made when you choose how to manage a
collection of many of them. I hope it's clear by now that simply
saying `ptr[0] = 2006' is not an adequate description of what you
are trying to accomplish; you need to be more specific.

--
Eric Sosman
esosman@acm-dot-org.invalid
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Fri Jul 21, 2006 1:15 pm    Post subject: Re: Adaptive datatype (or so...) Reply with quote

On Fri, 21 Jul 2006, Eric Sosman wrote:

Quote:
Whatever5k@web.de wrote:
Barry, your example would not work for a binary file.
OK, here is another example. Let's say I have got a binary file, that
contains the year and month number of today. Now, this would be written
with 0 and 1, but it would look like this: 200607.

Your description of the data format is still unclear
(to me, anyhow). Do you mean that the file contains the
number "two hundred thousand six hundred seven" as a
binary integer in the machine's native form (probably four
or eight bytes long)? What does "look like this" mean?

Ok, we would say
that one symbol occupies 4 bytes. So 2006 would be the first symbol and
^^^^^^^
07 the second.

It sounds like 07xx would be the second, where the x's
are two more bytes. What do you mean when you say 07 is
a four-byte "symbol?"

What I want to have is an array so that ptr[0] = 2006
and ptr[1] = 07.

It seems you don't realize that C supports many different
data types, and can represent 2006 in many different ways.
Some of them are

[snipped]

The OP has explicitly requested that his data type is to be
``4 bytes'' (underlined above) which according to the C standard
means an object containing 4 * CHAR_BIT bits. Therefore, the OP
was trying to say this:

char ptr[2][4] = {{'2', '0', '0', '6'}, {'0', '7'}};

when he/she wrote ``ptr[0] = 2006 and ptr[1] = 07''.

Tak-Shing
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 8:05 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Adaptive Server Anywhere 6.0 record deletion issue coppermineblueskyn@yahoo. Sybase 0 Mon Jul 17, 2006 1:27 pm
No new posts Insert to Clob datatype test 8x slower on 9207 vs 9205? Dave Stien Server 1 Wed Jul 05, 2006 2:15 pm
No new posts REF Datatype Khurram Server 3 Mon Jul 03, 2006 12:54 pm
No new posts CHAR datatype issue shsandeep IBM DB2 1 Wed Jun 28, 2006 8:17 am
No new posts text datatype ali asghar torabi parizy MySQL 1 Mon May 29, 2006 6:53 pm

Anniversary Gifts | Jokes | Mortgage Calculator | Loans | Singapore Shopping Guide
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.7941s ][ Queries: 16 (0.6467s) ][ GZIP on - Debug on ]