saving/retriving strctures from files

This is a discussion on saving/retriving strctures from files within the C Programming forums, part of the General Programming Boards category; i have an array of a a structure, called videos and i want to save it to file and then ...

  1. #1
    Unregistered
    Guest

    Question saving/retriving strctures from files

    i have an array of a a structure, called videos and i want to save it to file and then load it back in to memory later, so far i have the following code:

    //to open the file, fname being the file name, and opp being the mode either rb or wb

    tfile = fopen(fname, opp);

    //to write the structure to the file
    fwrite(&video, 8800, 1, tfile);
    fclose(tfile);

    this bit works i have looed in the file and it apers to contain the correct information, however the read bit dosent work, the code is the same except for this:

    fwrite(&video, 8800, 1, tfile);

    anyone know how to get this to work??

  2. #2
    Sayeh
    Guest
    You should not put a hardcode number in the function call-- you should always use the 'sizeof' statement.

    prototype:
    ----------
    fwrite(void *buffer,size_t elem_size,int num_elem,FILE *stream);

    buffer = your buffer filled with data-- this can be a single struct, or a single-dim'd array of struct of like-size.

    elem_size = size of individual structure. If you are writing an array of structs, this could simply be the size of the entire array.

    num_elem = number of individual structures/elements. If you were writing an entire array, then this could just be a one, so long as 'elem_size' is as big as all the structures in the array.

    stream = record for the file control block.

    ---

    So, instead of using fwrite the way you do, rewrite it like so:

    Code:
    typedef struct
       {
       int   yearMade;         /* when video made (c) */
       char actors[5][35];     /* actor names */
       long   runTime;         /* running time, minutes */
       char   rating[6];       /* movie rating */
       }videoStruc;
    
    int main(void)
       {
       videoStruc video;       /* declare vars */
       FILE tfile = 0L;
    
       ...
    
       fwrite(&video,sizeof(videoStruc),1,tfile);
       ...
       return(0);
       }
    fopen() is very nearly the same, just moving data in a different direction. In both cases, you should position the file pointer prior to reading/writing so that it is where you expect it. You should also be checking for errors.

    enjoy.

  3. #3
    Unregistered
    Guest

    Question

    i tried:

    fwrite(&video, sizeof(video), 1, tfile);
    and
    fwrite(&video, sizeof(video), 1, tfile);

    and it still does not read the file but it does write them.

  4. #4
    Unregistered
    Guest

    Red face

    never mind got it working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21