Thread: Binary Read and Writes...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Angry Binary Read and Writes...

    I'm forced to keep an array of 2000 elements on disk for later use..
    My program uses this :

    struct array //I do the struct to encapsulate -sort of anyway
    {
    char m_pCodes[1000];
    }array;

    Now in my program i write let's say 5 codes -at place [0]-1-2-3-4..

    I know wanna save them on disk and close the program.
    When I open the program i want to see the same codes(offcourse) and then afterwards append the array.. Say know i'll continue writing in element 5-6 and 7.

    My program though writes the file... But when reopen -data's smashed !!!!

    Please help me on this one.. Since i'm not a huge fan on Console programming -file access has never bin much of intereset...

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Code:
    FILE *f  = fopen("foo.bar", "wb");
    fwrite(array, sizeof(array), 1, f);
    fclose(f);
    ...
    FILE *f = fopen("foo.bar", "rb");
    fread(array, sizeof(array), 1, f);
    fclose(f);
    BTW, that really doesn't make sense to put your m_pCodes inside a structuce. And - don't know how that `encapsulation' effects on it - you could just open it in non-binary -mode 'cause the actual data is char.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Lightbulb Well...

    The Encap... is to categorize the variables... !
    So the next programmer who looks might get a quicker overview
    of the code.

    Well it's still messed up. What if I just wanna write the array ?

    like fread(array,array[10000],1,fptr); //???

    Should work or ?

    It must remember number [3] f.ex.

    I use strdup, so the mem alloc problem should be solved..

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Unhappy And...

    Since i'm handling a very big chunk of data -it would be sloppy to write it in pure ascii.
    Rather in binary... Because i'm able to handle huge amounts of data -while the ascii or single fprintf, fscanf functions don't reach my requirements ;(

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What if I just wanna write the array ?
    Do as Kooma showed you

    The "rb" and "wb" are important

    You mentioned strdup, which involves memory allocation, so, if
    struct array //I do the struct to encapsulate -sort of anyway
    {
    char m_pCodes[1000];
    }

    contains pointers, you need a different approach

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Arrow Okay....

    Okay i've studied the code again... And see what you mean.. :=

    If I do this:

    char array[1000][12];
    //String is 12 chars long and there are 1000 of them

    I can now do an ordinary strcpy -because the memory is allocated this way...

    Now i need to write alle the codes down into a binary file -and shutdown -reopen -place codes into array again (sort of "restore")

    Can i then do this ?

    while(n<MAX)
    fread(array[n][~],sizeof(array[n][~]),1,fptr);

    and the same way around with fwrite ?

    -> By the way the b in "wb"-"rb" ~well offcourse... it wouldn't be binary else...

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    why do you don't write all at once??

    fwrite(array,1,12*1000,file);

    and read at once:

    fread(array,1,12*1000,file);

    mhm, but I think you shouldn't use the stack for such big arrays... allocate the memory on the heap using malloc()
    Hope you don't mind my bad english, I'm Austrian!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can i then do this ?
    > while(n<MAX)
    > fread(array[n][~],sizeof(array[n][~]),1,fptr);

    That's one way
    But that only does one char at a time, which seems pointlessly inefficient

    And in any event, it would need to be
    > fread( &array[n][~],sizeof(array[n][~]),1,fptr);


    To do the whole array in one go, its
    fread( array, sizeof(array), 1, fptr );

    Or a line at a time
    for ( i = 0 ; i < 1000 ; i++ )
    fread( array[i], sizeof(array[i]), 1, fptr );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-17-2009, 04:41 PM
  2. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  3. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  4. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 07:15 AM
  5. Read and write hanging
    By zee in forum C Programming
    Replies: 8
    Last Post: 08-03-2004, 11:19 PM