Syntax for writing a union to file

This is a discussion on Syntax for writing a union to file within the C++ Programming forums, part of the General Programming Boards category; I'm new to all this so please be gentle..... I have a union of structures(below) and the union must be ...

  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, 01: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, 11:12 PM

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