Save data from two struct arrays in one .dat file

This is a discussion on Save data from two struct arrays in one .dat file within the C Programming forums, part of the General Programming Boards category; hi! i'm trying to create a "backup" feature in my school project. what i want to do is, save all ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Save data from two struct arrays in one .dat file

    hi!

    i'm trying to create a "backup" feature in my school project. what i want to do is, save all the data from the two struct arrays that i'm using into one .dat file.

    how can i do it?

    Code:
    Medico listaMedico[mTAM]; //<---- struct array #1
    Paciente listaPaciente[pTAM]; //<---- struct array #2
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Open file.
    fwrite(listaMedico, sizeof(listaMedico), 1, file_handle);
    Repeat.
    Close file.
    Not portable, of course, but it's the easiest way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    humm...

    if i do this:
    Code:
    fwrite(listaMedico, sizeof(listaMedico), 1, file_handle);
    fwrite(listaPaciente, sizeof(listaPaciente), 1, file_handle);
    won't it replace the data from listaMedico with the data from listaPaciente?

    And how do i copy the data from the file to the listaMedico and listaPaciente struct array?
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    No, the file pointer is adjusted with each write. So the data written after the first write is placed directly after the first.
    And when you want to read it back, you just reverse the process:

    Open file.
    fread(listaMedico, sizeof(listaMedico), 1, file_handle);
    fread(listaPaciente, sizeof(listaPaciente), 1, file_handle);
    Repeat.
    Close file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    wow! that simple?
    nice :-D

    thanks Elysia!
    "Artificial Intelligence usually beats natural stupidity."

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    That simple.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

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