CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: file sorting

  1. #1
    Join Date
    Nov 2002
    Posts
    23

    file sorting

    Hi all,

    this is actually more of a Unix question .... (maybe).

    Within my C++ code i'd like to sort a file based on a particular field. The file has *no field delimiters* but i know the byte length of each field. The only way i can think of doing this effeciently is using a system call to Unix's "sort". However from what i can tell sort doesn't work when sorting by a particular field, unless the file is field delimited or has at least one blank space between fields. My file has neither.

    So my question is, is there effecient way to sort this file using either C++ code or System calls to unix commands?
    (and again the sorting is on a particular field)

    Also, why doesnt unix allow sorting based on a byte position (byte interval)???

    thanks as always,
    lil hasher

  2. #2
    Join Date
    Jan 2001
    Posts
    588
    If I'm understanding your problem correctly, I'm assuming that you have a data file that has a number of fields for each data point, with a sequence of data points repeated throughout the file. If this is true, you could do this:

    1. Create a class or struct that contains the members of each data field.

    2. Parse the data file, reading each data value into an instance of the class you created. Insert each one into a std::vector<yourClass>.

    3. Write a predicate that compares objects of your class based on the field you want to sort on.

    4. Use std::sort to sort the vector's contents.

    5. If necessary, output the objects one by one back into the data file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured