Solved

Sorting a binary File

Posted on 2004-10-02
4
241 Views
Last Modified: 2010-04-01
Along the same lines of my other question, I also want to have it do this: I have a program that writes error information to a binary file. Each error code has an ID and alot of other information with it. Since the legacy app shows the error code but not the info, I want to write a command line program that will sort all of the of the information on the ID code and display the contents of the whole file to me sorted.
0
Comment
Question by:microcoop
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12211435
0
 

Author Comment

by:microcoop
ID: 12211804
I posted both questions, this isn't duplicate the other one is searching but this one is sorting.
0
 
LVL 3

Expert Comment

by:CmdrRickHunter
ID: 12212834
You dont "sort a file," you read in the data from a file, sort it, and display it.

pseudo code:

open the file
read in all the "records" into an array (or other container)
close the file
sort the records by whatever criteria you want
output all the information you want
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 12212851
What you need is an memory index, either if records are fixed- or variable lenght, the index will provide you a fast access to your file, and allow you to sort fastly.
You have 2 alternatives of indexes in mi opinion:
Offset only.- The index array will contain the file position of the init of every record. Every time you want to access a record randomly, go to de index, retrieve offset, and jump into the file, read record data, reading key first.
Offset and key.- The index array will contain also the key or ID of every record, so you don't have to read the file to locate or even sort the file. Once you have located the record by index in the index, then load rest of data into memory, it is more efficient but comsumes more memory, if file is not so bigger, then it is a good alternative.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now