Thread: To sort an input file and write it as multiple files

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    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

    The code should be in c++.
    Last edited by kavithanarayan; 03-14-2010 at 07:44 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    what's your plan for doing that?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Code:
    FunctionNameThatGoesThroughAFileAndOutputsAllLinesStartingWithALetterToASeparateFile(var infilename, var letter, var outfilename) {
    	var InFile
    	var Line
    	var Vector
    	var OutFile
    
    	InFile = OpenFile(infilename)
    	While(!InFile.Eof) {
    		Line = InFile.GetLine
    		If Line.StartsWith(letter) {
    			Vector.Add(Line)
    		}
    	}
    
    	Vector.Sort
    
    	OutFile = OpenFile(outfile)
    	While(!Vector.Eof) {
    		OutFile.WriteLine(Vector.Line)
    	}
    
    	OutFile.Close
    	InFile.Close
    }
    
    Main {
    	From a To z {
    		FunctionNameThatGoesThroughAFileAndOutputsAllLinesStartingWithALetterToASeparateFile(input.txt, letter, letterLETTER.txt)
    	}
    	From A to Z {
    		FunctionNameThatGoesThroughAFileAndOutputsAllLinesStartingWithALetterToASeparateFile(input.txt, letter, letterLETTER.txt)
    	}
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    While(!InFile.Eof)
    bad idea to use eof to control loop - read FAQ for explanaition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.

    P.S. I hope you weren't serious about your function name!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Hi friends,

    Thankyou for your reply.

    I found it difficult to sort the files.

    I want to sort the Aa.txt files that contain:

    "Apple"
    "Ant"
    "america"
    "append" to:


    "america"
    "Ant"
    "append"
    "Apple"


    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM