Thread: Syntax for writing a union to file

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    Syntax for writing a union to file

    I'm new to all this so please be gentle.....
    I have a union of structures(below) and the union must be written to a binary file.

    union URec{
    struct one structA;
    struct two structB;
    struct three structC;
    };
    union URec Data;

    I need the syntax for writing the union to file...anyone????
    cheers Dunc.

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Code:
    #include <fstream>
    using namespace std;
    
    ofstream out("file.txt", ios::out|ios::bin);
    out << data.whatever; etc etc

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <fstream>
    using namespace std;
    .
    .
    .
       ofstream out("file.txt",ios::binary);
       out.write((char *) &Data,sizeof(Data));
       out.close();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM