How do I write an array to a file?

Discussion in 'Mac Programming' started by farmerdoug, Jan 11, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    If I've allocated memory for an array[1500][5][45][8] , how do I write to a file using fwrite?
    This doesn't work.
    fp = fopen(filename, "w");
    fwrite((char ****)Caverages, sizeof((char ****)Caverages), 1, fp);
    fclose(fp);
     
  2. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #2
    or maybe I not reading it right.

    fp = fopen(filename,"r");
    i = fread((char ****)Cminus1, sizeof((char ****)Cminus1), 1, fp);
    fclose(fp);
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    This is not doing what you think. You're writing the first few bytes (sizeof(void *)) at the beginning of the array. You need to write in a loop if this isn't one contiguous block of memory. If it is you need to write sizeof(char)*sizeA*sizeB*sizeC*sizeD where the sizes are your four dimensions.

    -Lee
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    how do I write ...

    I allocated memory with calloc so it should be a single block. I'll give it a try in the morning. Thanks.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    So you did one calloc for 2700000 elements? In previous posts it looked like you were allocating a dimension at a time, so it wouldn't be in a single block. Maybe you've changed your methodology, but you need to know the layout of the memory to write it correctly.

    -Lee
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    I suggest using printf() to display that value.

    I suggest doing a similar thing with your sizeof() expression involving Caverages.

    I also question why you're casting like this:
    Code:
    fwrite((char ****)Caverages, ...snip...
    and this:
    Code:
    fread((char ****)Cminus1, ...snip..
    The declared type of that arg for fwrite() and fread() is void*. Therefore, you should be casting to void*, if you're going to cast to anything. Which makes me wonder what the types of Caverages and Cminus1 actually are, that you need to cast them (or think you do).


    It depends on how the array is declared, and how the memory is actually layed out.

    In C, multi-dimensional array subscripting, such as array[j] can have two distinct memory layouts:

    1. If the variable is declared as an actual array, then the memory layout is row-major order in linear memory.

    http://en.wikipedia.org/wiki/Row-major_order

    2. If the variable is declared as an array of pointers (or as a pointer to pointers or arrays), then the memory layout is non-linear and is more like a "vector of pointers" (where vector just means a one-dimensional array). Technically, the columns (first subscript) are pointers, which point to rows (second subscript) at arbitrary memory locations. Because the columns are pointers (a vector of pointers), the memory layout is both non-linear and contains memory addresses. Those addresses will not survive a round-trip read-after-write to a file. That is, the addresses written to a file will be vacuous when read back in.

    In C, any pointer can be subscripted, and every unsubscripted array reference is a constant pointer to the first element of that array.

    That's probably not the most accurate summary, but it's the best I can do for now.


    Please describe exactly how it doesn't work. If it does anything at all, exactly what does it do?

    There are countless ways for something to not work. We can't reach into your computer and read your files, or look at your debugging history and see what didn't work, or watch how you got there. You have to be specific about your results, both the results that work and the results that don't.

    http://www.mikeash.com/getting_answers.html


    All in all, from everything you've posted so far, you seem unclear on exactly how C memory and arrays work. You might be better served in the long run by learning and using a language with a builtin string type, such as Perl or Python. Perl, in particular, was created for doing string-parsing and processing tasks.
     
  7. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    I figured that out.
    thanks.

    I plead guilty to lack of knowledge. I'm not a full time C programmer.
    I need to save a 3d array of strings for use in later programs. Since I have already allocated the memory in loops using calloc, I'm going to try Lee's suggestion of writing to a file in loops. In seems to make the most sense now that I know a little more than yesterday.

    The declared type of that arg for fwrite() and fread() is void*
    I always thought that one should substitute the actual data type for void.

    thanks.
     

Share This Page