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

    Merging text files based on headings.

    I am looking for some advice/pseudo code on how I could merge multiple text files based on their headings.

    So say I have the following 3 text files with the following headings. As you can see some elements repeat and some are unique to the text file. The text files contain thousands of rows under each header containing various types of data.

    Text File 1:

    Element1|Element2|Element4|Element5|
    00000001|00000002|00000004|00000005|

    Text File 2:

    Element2|Element3|Element4|Element5|
    00000002|00000003|00000004|00000005|

    Text File 3:

    Element1|Element3|Element4|Element6|
    00000001|00000003|00000004|00000006|

    The final output Text File would would look something like this:

    Element1|Element2|Element4|Element5|Element3|Element6|
    00000001|00000002|00000004|00000005|00000003|________|
    ________|00000002|00000004|00000005|________|________|
    00000001|________|00000004|________|00000003|00000006|

    As you can see the output text file would capture every header from the original 3 Text Files. It would then store each data field under the appropriate heading. If an element is not available in a particular Text File then the field is left empty in the final output file.

    Thank you!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Merging text files based on headings.

    Do this in a few steps:

    Code:
    First, read the first line in all files to build up a 'master' header list (you'll want to track each of the columns  Element1,Element2,Element3,Element5,Element6,Element9,Element10, etc. in all the files)
    
    Next, create the output file and insert the 'master' header columns from the previous step as the first line
    
    Next, for each file...
      Open the file
      Extract the header information and compare this header information with the master header to determine where to add spaces
      For each line
        Read each line
        Form the new data line by extracting each column and build up the new line by inserting spaces where necessary (based on the header/master header compare)
        Write the new line to the output file
      Loop
    Loop

Tags for this Thread

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