Thread: Putting a structure into a file

  1. #1
    Unregistered
    Guest

    Putting a structure into a file

    I have a structure i need put into a file I know that you can do binary files but when i use fopen, fwrite, the open works but fwrite always causes the program to crash. Can someone post an example of creating, writing and reading a structure from a binary file?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Why don't you post your code that crashes? That way we can see whats wrong.

    And this topic should be in the C forums.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Unregistered
    Guest
    Code:
    FILE *stream;
     if ((stream = fopen("binary.dat", "wb")) != NULL)
     {
       fwrite(&data_struct, sizeof(data_struct), 1, stream); // write struct s to file
       fclose(stream);
     }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hmm, that looks correct, assuming that your structure is...

    Here is a write/read sample which works for me: try and compile it. If it crashes it must be something with your compiler or OS.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct
    {
       int Var1;
    }MYSTRUCTURE;
    
    int main()
    {
       MYSTRUCTURE Structure1;
       FILE* WriteFile;
       Structure1.Var1=12345;
    
       WriteFile=fopen("Myfile.tmp", "wb");
       if(WriteFile!=NULL)
       {
          //Write data
          fwrite(&Structure1, sizeof(MYSTRUCTURE), 1, WriteFile);
          fclose(WriteFile);
       }
       else
       {
          //Error
          printf("Error writing file!");
          getch();
          return 0;
       }
    
       MYSTRUCTURE Structure2;
       FILE* ReadFile;
    
       ReadFile=fopen("Myfile.tmp", "rb");
       if(ReadFile!=NULL)
       {
          //Read data
          fread(&Structure2, sizeof(MYSTRUCTURE), 1, ReadFile);
          fclose(ReadFile);
       }
       else
       {
          //Error
          printf("Error reading file!");
          getch();
          return 0;
       }
    
       //Print the result
       printf("value: %d", Structure2.Var1);
       getch();
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Code:
    ifstream fin;
    fin.open("filenamehere");
    fin>>data_struct;
    fin.close();
    the include file is process.h




    ... damn you guys try too hard. its this simple
    Last edited by Denethor2000; 04-22-2002 at 10:35 AM.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Denethor2000
    Code:
    ifstream fin;
    open("filenamehere");
    fin>>data_struct;
    close();
    the include file is process.h
    This doesn't look right. Shouldn't the second line be
    fin.open("FileName.ext");
    And the last line be:
    fin.close();
    ???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    yeah i caught that
    its edited, refresh

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Unregistered... you are welcome..... weeeeeeeell? if u ask around ull ifnd im very conscious of people being GRATEFUL, YOU UNG-... yeah. you're welcome

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    How can you add and load char[]'s to/from a structure then?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct
    {
       char[10] Var1;
    }MYSTRUCTURE;
    
    int main()
    {
       MYSTRUCTURE Structure1;
       FILE* WriteFile;
       Structure1.Var1="sghrdhj";
    
       WriteFile=fopen("Myfile.tmp", "wb");
       if(WriteFile!=NULL)
       {
          //Write data
          fwrite(&Structure1, sizeof(MYSTRUCTURE), 1, WriteFile);
          fclose(WriteFile);
       }
       else
       {
          //Error
          printf("Error writing file!");
          getch();
          return 0;
       }
    
       MYSTRUCTURE Structure2;
       FILE* ReadFile;
    
       ReadFile=fopen("Myfile.tmp", "rb");
       if(ReadFile!=NULL)
       {
          //Read data
          fread(&Structure2, sizeof(MYSTRUCTURE), 1, ReadFile);
          fclose(ReadFile);
       }
       else
       {
          //Error
          printf("Error reading file!");
          getch();
          return 0;
       }
    
       //Print the result
       printf("value: %d", Structure2.Var1);
       getch();
       return 0;
    }
    Certainly doesn't work. Even if it did, the characters of the string would be read with their numerical values.

    I need to know how to do this because I want to add and load questions, example:

    Code:
    struct Database
    {
        int history_qs_n_ans_number;
    
        char history_qs[1000];
        char history_ans[1000];
        char note[1000];
    };
    
    Database Religious_history[] =
    {
    
      { 1, "Who founded the religion of Zoroastrianism?", "Zarathustra", "The belief of when Zarathustra, or Zoroaster lived, has varied greatly over the years, he is now dated to about 1000 BC. There are some people who even questionhis existence." },  // #1 initialized.
      { 2, "Which was the official religion in the empire of Han(206 BC - 220 AD)?", "Konfucianism", "Konfucianism remains from the belief of the philiosoph Konfucius" },  // #2 initialized.
      { 3, "Which year did Muhammed escape from Mecca?" }   // #3 initializexd.
    
    };
    How can all this succefully be added and loaded from/to a file?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Structure1.Var1="sghrdhj";

    This should be:
    strcpy(Structure1.Var1,"sghrdhj");

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can you add and load char[]'s to/from a structure then?
    Yeah but not the way you want. You can't use a static table like that. Well, you could:

    Code:
    struct mydata mytable[MYSIZE];
    for( x = 0; x < MYSIZE; x++ )
        fread( mytable[x], sizeof( struct mydate ), 1, fp );
    Or you could just use a linked list.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM