-1

The code below reads a CSV file and looks for a line containing the serial number which is the first column of the file. Then copies that line to another file. The code works fine.

I need to read the text in the second and third fields of the row (there are 12 fields) and assign them to string variables (for other uses).

Can you help me, Please. I am a novice.

List<string> found = new List<string>();

string line;
using(StreamReader file = new StreamReader(input_filename))
{
    while((line=file.ReadLine())!=null)
    {
        if(line.Contains("XA2345")) // Serial Number 
        {
            found.Add(line);

            using(StreamWriter w = File.AppendText(output_filename))
            {
                // Console.WriteLine(line);
                w.WriteLine(line);
                w.Flush();
            }
        }
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 6
    Possible duplicate of [Reading CSV files using C#](https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp) – GSerg Dec 01 '19 at 20:16
  • @FrancescoC before posting question have some courtesy to search for answers using the top search bar – Nitin Sawant Dec 02 '19 at 01:09

1 Answers1

0

I'd start with some best practices for parsing CSV files with the post Parsing CSV files in C#, with header.

I've also noticed you've got that "found" variable. Are you trying to avoid duplicate lines in your output file but the code is incomplete? I've written the following code under that assumption.

Here are the using statements:

using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Here's the main code:

List<string> foundLines = new List<string>();
using (TextFieldParser parser = new TextFieldParser(inputFilename))
{
    // Set up the parser for CSV files
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");

    using (StreamWriter writer = new StreamWriter(outputFilename, false))
    {
        while (!parser.EndOfData)
        {
            string[] values = parser.ReadFields();
            string serialNumber = values[0];

            if (string.Equals(serialNumber, "XA2345"))
            {
                string line = string.Join(",", values.Select(Escape));
                if (foundLines.Contains(line))
                    continue; // Skip writing this line more than once
                else
                    foundLines.Add(line); // Remember this line for later

                writer.WriteLine(line);

                // Do what you need to with the individual column values
                string secondValue = values[1];
                string thirdValue = values[2];
                // ... Etc. ...
            }
        }
    }
}

And here's a CSV helping method for escaping values as needed at Good CSV writer for C#:

static private string Escape(string s)
{ 
    const string QUOTE = "\""; 
    const string ESCAPED_QUOTE = "\"\""; 
    char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
    if (s.Contains(QUOTE))
        s = s.Replace(QUOTE, ESCAPED_QUOTE);
    if (s.IndexOfAny(CHARACTERS_THAT_MUST_BE_QUOTED) > -1)
        s = QUOTE + s + QUOTE;
    return s;
}
andyrut
  • 134
  • 7