"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.