Thread: VC++ Sort File

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

    Unhappy VC++ Sort File

    // this is my union......
    union URec {
    structA astruct; //union cont. 3 structs
    structB bstruct;
    structC cstruct;
    };
    union URec Data;

    // The unions are held on a binary file and require reading into an array to be sorted before being written to a text file. The arrays are sorted in ascending order of the the second field of each structure.
    I have used the following code to try to achieve this but I am not sure about how to sort the array or how to write it to the text file.
    Any help, ideas or criticism(constructive) would be hugely appreciated.

    infile.seekp(0, ios::end);
    totnum = infile.tellg(); // No. of bytes in file
    elements = totnum / sizeof(Data); //No of unions in file
    URec *code_ptr;
    code_ptr = new URec [elements]; //assign new memory
    // read unions into array
    infile.read((char *)code_ptr,sizeof(elements * sizeof(Data)));

    // start sorting ????????????

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Are you allowed to use a standard library sort function?

    Does your text file have to be human readable? If so you'll have to write each element of whatever structure has been read separately using something like operator <<.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM