Solved

Sorting a binary File

Posted on 2004-10-02
4
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

751 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