Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0 CSV
I formated this data using c#, streamreader and writer and created this csv file.   Here is the sample data:
 
A------B-C-----D------------E------------F-------G-------H---------I
NEW   C A123 08/24/2011 08/24/2011   100.00 100.00 X123456 276135
NEW   C A125 08/24/2011 08/24/2011   200.00 100.00 X123456 276135
NEW   C A127 08/24/2011 08/24/2011      50.00 100.00 X123456 276135
NEW   T A122 08/24/2011 08/24/2011         5.00 100.00 X225511 276136
NEW   T A124 08/24/2011 08/24/2011      10.00 100.00 X225511 276136
NEW   T A133 08/24/2011 08/24/2011   500.00 100.00 X444556 276137
 
I would like the following output:
 
NEW   C A123 08/24/2011 08/24/2011   100.00 100.00 X123456 276135
NEW   C A125 08/24/2011 08/24/2011   200.00 100.00 X123456 276135
NEW   C A127 08/24/2011 08/24/2011      50.00 100.00 X123456 276135
NEW   C A001 08/24/2011 08/24/2011   350.00 100.00 X123456 276135
NEW   T A122 08/24/2011 08/24/2011         5.00 100.00 X225511 276136
NEW   T A124 08/24/2011 08/24/2011      10.00 100.00 X225511 276136
NEW   T A001 08/24/2011 08/24/2011      15.00 100.00 X225511 276136
NEW   T A133 08/24/2011 08/24/2011   500.00 100.00 X444556 276137
NEW   T A001 08/24/2011 08/24/2011   500.00 100.00 X225511 276137
 
With each change in field "I", I would like to add a line, sum column F, add a "A001" to C, and copy the contents
of the other fields into that newly ADDed line.   The letters on the columns are for illustrative purposes only.   There are no headers.
 
First, what should I do first?   How do I sum column F, copy contents of all fields, and add "A001" to C?   How do I add a line and copy the fields w/ each change in I?
 
Thank you in advance for your help.
Posted 1-Sep-11 13:58pm
DPBNYC320
Edited 1-Sep-11 14:12pm
v4
Comments
DPBNYC at 1-Sep-11 21:03pm
   
I know I left the commas out. Visualize them please.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You commas are visualized Smile | :) . I feel pretty generous today. Do the following:
 
  1. Create a class or structure representing one line (record); let's call it Record.
  2. Use the list of those records. Literally, add:
    using RecordList = System.Collections.Generic.List<Record>;
  3. For reading a file, use System.IO.StreamReader; create it under "using" statement:
    RecordList list = new RecordList();
    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName) {
        //read lines here:
        string line = reader.ReadLine();
        //parse and validate each line
        Record record = //create instance of Record from parsed line
        list.Add(record);
    } //at this point, reader.Dispose will be called automatically, even if an exception was thrown
    
  4. Write a method of reading one line from a record, parsing and validating it. (It's shown as a comment above.) Start parsing using string.Split:
    //...
    string[] values = line.Split(',', System.StringSplitOptions.RemoveEmptyEntries);
    //convert strings into field of the type Record
    //use DateTime.Parse, double.Parse, uint.Parse, etc. See appropriate MSDN help pages
    //validate values as you parse
    //for example:
    record.F = double.Parse(values[5]); //may throw exception; decide how to handle it
    
  5. For finding sums, iterate through the list, for example:
    double sumF = 0d;
    /...
    foreach (Record record in list) {
        sum.F += record.F;
        //...
    }
    
  6. In case you might need an array for some other purposes, the previous step may look alternatively:
    Record[] records = list.ToArray();
    double sumF = 0d;
    /...
    foreach (Record record in records) {
        sum.F += record.F;
        //...
    }
    
  7. PROFIT!
 
—SA
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 OriginalGriff 258
2 ChintanShukla 195
3 Bhavik_Patel 170
4 Richard Deeming 165
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 2 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100