Removing and updating a column in a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snathan
    New Member
    • Jan 2007
    • 3

    Removing and updating a column in a text file

    I have text file where the contents are tab delimited and are as follows (without any headers)
    A B C D E
    05 16 99 21 .0000
    05 16 99 21 .0000
    05 16 99 99 .0200
    05 99 99 99 .0140

    I need to generate a text file of this format
    A B D New E
    05 16 21 99 .0000
    05 16 21 99 .0000
    05 16 99 99 .0200
    05 99 99 99 .0140

    Column C from the original txt file should be skipped, D from the new file should match D from the old file and a new coulmn to be added with default value of 99.

    Please let me know how to do it...
  • enreil
    New Member
    • Jan 2007
    • 86

    #2
    You should be able to read the file into an Arraylist, insert columns into the arraylist wherever you like, and then write the contents of the arraylist back to the text file. I've done similar tasks with Arraylists and/or Arrays before and it is pretty slick.

    Comment

    • snathan
      New Member
      • Jan 2007
      • 3

      #3
      Originally posted by enreil
      You should be able to read the file into an Arraylist, insert columns into the arraylist wherever you like, and then write the contents of the arraylist back to the text file. I've done similar tasks with Arraylists and/or Arrays before and it is pretty slick.
      I did it thru streamreader and stream writer...but can u send me a sample c# code which has the arrayllist method...

      Comment

      • enreil
        New Member
        • Jan 2007
        • 86

        #4
        I don't have the code now, and I did it in VB.NET, so it may not help you.

        You'd need a 2-d array to handle this. The first dimension handles the rows, the second handles the columns. Open your file with the streamreader and read each row into the arrays. Once that is complete, you can add/edit/delete any data you wish by iterating through positions in the arrays.

        I'm glad you accomplished your task a different way. The one I suggested was the first that came to mind because I've used it, but there are usually many ways to approach a problem like this.

        Note that you could use arraylists in the above logic instead of arrays. Though there is controversy over which is more efficient in which circumstance, unless you are dealing with thousands of rows it does't make too much practical difference.

        Originally posted by snathan
        I did it thru streamreader and stream writer...but can u send me a sample c# code which has the arrayllist method...

        Comment

        • snathan
          New Member
          • Jan 2007
          • 3

          #5
          the txt file that i would be getting contains about a million records...so thatz y i wanted to know which method would be better without affecting any performance...

          Comment

          Working...