Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a binary File

Posted on 2004-10-02
4
Medium Priority
?
250 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 1000 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

Technology Partners: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

824 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