Dynamic arrays help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikesh

    #1

    Dynamic arrays help

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

  • Vladimir S. Oka

    #2
    Re: Dynamic arrays help

    Nikesh wrote:[color=blue]
    > project is abt encrypting a txt file with an image....[/color]

    The words you were looking fore are "about" and "text"...
    [color=blue]
    > 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[/color]

    Why would size "keep on changing" if you load the file only once?
    [color=blue]
    > need to keep that array flexible but exact to the txt thats loaded or
    > given by the user...how to do this part[/color]

    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().
    [color=blue]
    > 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)..[/color]

    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

    Comment

    • websnarf@gmail.com

      #3
      Re: Dynamic arrays help

      Nikesh wrote:[color=blue]
      > 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)..[/color]

      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
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      Comment

      • SM Ryan

        #4
        Re: Flexamic arrays help

        "Nikesh" <nike83@gmail.c om> 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(Fle x##T A) { \
        free(A.a); \
        } \
        \
        int lengthFlex##T(F lex##T A) { \
        return A.n; \
        } \
        \
        T* arrayFlex##T(Fl ex##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(stdi n))!=EOF) text = putFlexchar(tex t,lengthFlexcha r(text),ch);
        text = putFlexchar(tex t,lengthFlexcha r(text),0);

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

        Comment

        Working...