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

Dynamic arrays help

P: n/a
project is abt encrypting a txt file with an image....

in that i will to accept a txt file from the user ..this file a need to
be stored in an array...thus file size will keep on changing....and i
need to keep that array flexible but exact to the txt thats loaded or
given by the user...how to do this part

each array element will have one char of text...

then this char is converted into ASCII --> binary --> and stored in
other array..(each element will have just have one bit)..

Feb 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Nikesh wrote:
project is abt encrypting a txt file with an image....
The words you were looking fore are "about" and "text"...
in that i will to accept a txt file from the user ..this file a need to
be stored in an array...thus file size will keep on changing....and i
Why would size "keep on changing" if you load the file only once?
need to keep that array flexible but exact to the txt thats loaded or
given by the user...how to do this part
Use malloc() based on the size of the file to allocate the space you
need. If you really need to change the size of that space later on, use
realloc().
each array element will have one char of text...

then this char is converted into ASCII --> binary --> and stored in
other array..(each element will have just have one bit)..


You cannot store a char in just one bit. The Standard requires a char
to be at least 8 bits.

Hopefully, this helps you, but your post is quite confused, and I'm
still not entirely clear what you really want, and what your problem
is. You'll get more, and better help if you don't use silly word
abbreviations, and do use proper capitalisation. Also, organising your
thoughts before actually typing helps a lot.

--
BR, Vladimir

Feb 6 '06 #2

P: n/a
Nikesh wrote:
project is abt encrypting a txt file with an image....

in that i will to accept a txt file from the user ..this file a need to
be stored in an array...thus file size will keep on changing....and i
need to keep that array flexible but exact to the txt thats loaded or
given by the user...how to do this part

each array element will have one char of text...

then this char is converted into ASCII --> binary --> and stored in
other array..(each element will have just have one bit)..


Well you are using some interesting language to describe what you want
-- but it turns out that "The Better String Library" (Bstrlib) can do
that easily and very robustly. You basically want a dynamic array of
characters and a dynamic array of bytes. "bstrings" in Bstrlib make no
distinction between bytes and characters and treats '\0' (and any other
binary value) as a regular character; the primary preoccupation of
"bstrings" is to be a dynamically sized string. You can encrypt the
data directly either inplace, or copied or whatever you like. If you
don't have the memory for the whole txt file, you can use a bsStream
instead.

Bstrlib is open source and available here: http://bstring.sf.net/

--
Paul Hsieh
http://www.pobox.com/~qed/

Feb 6 '06 #3

P: n/a
"Nikesh" <ni****@gmail.com> wrote:
# project is abt encrypting a txt file with an image....
#
# in that i will to accept a txt file from the user ..this file a need to
# be stored in an array...thus file size will keep on changing....and i
# need to keep that array Flexible but exact to the txt thats loaded or
# given by the user...how to do this part

You can set up a flexible array with something like

#define Flex(T) \
\
typedef struct {T *a; int m,n;} Flex##T; \
\
Flex##T initialFlex##T(void) { \
T v = {0,0,0}; return v; \
} \
\
void freeFlex##T(Flex##T A) { \
free(A.a); \
} \
\
int lengthFlex##T(Flex##T A) { \
return A.n; \
} \
\
T* arrayFlex##T(Flex##T A) { \
return A.a; \
} \
\
T getFlex##T(Flex##T A,int i) { \
if (0<=i && i<A.n) return A.a[i]; \
else abort(); \
} \
\
Flex##T putFlex##T(Flex##T A,int i,T v) { \
if (i<0) abort(); \
if (i>=A.m) { \
A.m = 2*i+1; A.a = realloc(A.a,A.m*sizeof(T)); \
if (!A.a) abort(); \
} \
if (i>=A.n) A.n = i+1; \
A.a[i] = v; \
return A; \
}

Flex(char)
Flexchar text = initialFlexchar();
int ch;
while ((ch=fgetc(stdin))!=EOF) text = putFlexchar(text,lengthFlexchar(text),ch);
text = putFlexchar(text,lengthFlexchar(text),0);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Feb 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.