To sort an input file and write it as multiple files

This is a discussion on To sort an input file and write it as multiple files within the C++ Programming forums, part of the General Programming Boards category; Hi friends,
As part of my mini project, there is a portion where i need to:
a. Read an input ...

To sort an input file and write it as multiple files

Hi friends,

As part of my mini project, there is a portion where i need to:

a. Read an input file input.txt - The file will have 100 - 200 lines.
b. Segregate the data based on the first character of each line and
write it into separate files i.e. all the lines starting with alphabet
'a' or 'A' should be in file Aa.txt.
c. The data in each file should be sorted in alphabetical order
d. The input file name should be accepted as a command line parameter

Firstly, I commend you for making a psuedocode plan of what you intend to code up before starting the coding.
Secondly, thanks to the above I can point out a major performance related flaw in your design.
The program will take about 52 times longer than it should! On a non-performance related note, I believe you are going to produce incorrectly sorted output as per your current plan because your psuedocode shows you sorting the A's separately and outputting those, followed by all the a's (lowercase), sorted. What is probably asked for I believe, is for all the A's and a's to be sorted together so as to order all lines by their case-insentive ordering.

What you should do is modify your plan to only go through the file once. As it reads each line, you look at the first letter and store that in one of 26 vectors depending on the letter. Once the file reading is complete, sort all of the vectors in that array, and then output each sorted vector to its own file, one at a time.

This sorting should be done for each files like Bb.txt, Cc.txt containing lines starting with B/b and C/c respectively........that means a loop should be there to do the sorting of each files.
Then also if there is no file like Cc.txt then it should check the next file Dd.txt and sort it automatically.

The sorting should be done on the data in memory if possible. You sort each vector, and this doesn't matter if it's empty or not. Then you loop over the array again and create a file for each vector that is not empty. Sound okay?