Dynamic arrays help

N

Nikesh

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)..
 
V

Vladimir S. Oka

Nikesh said:
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.
 
W

websnarf

Nikesh said:
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/
 
S

SM Ryan

# 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; \
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 = 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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top