is there any way to read a csv file into a matrix, so every square in the file will be a cell in the matrix?
- 
                    what do you mean by matrix? a grid in a UI? if so, which UI framework? If not, what type of data structure? – James Manning Aug 18 '10 at 06:57
- 
                    @James - I think he means an array in a matrix format. `string[,]` – Jesper Palm Aug 18 '10 at 06:59
- 
                    right! just that i want an int matrix, but casting is not a problem here... – aharon Aug 18 '10 at 07:25
4 Answers
There are many open source CSV readers, and it's also easy to code your own.
For a start take look at codeplex.com: http://kbcsv.codeplex.com/
Or Codeproject tutorials: http://www.codeproject.com/KB/database/CsvReader.aspx
For sake of completion, here is my own utility class to read a line from a CSV file:
    /// <summary>
    /// Defines CSV reader states
    /// </summary>
    enum State
    {
        Initial, 
        Quote,
        Data,
        NestedQuote
    }
    /// <summary>
    /// Initializes a new instance of the <see cref="CsvReader"/> class.
    /// </summary>
    /// <param name="inputStream">The input stream.</param>
    public CsvReader(Stream inputStream)
    {
        if (inputStream == null) 
            throw new ArgumentNullException("inputStream");
        reader = new StreamReader(inputStream);
    }
    /// <summary>
    /// Reads a single line of CSV data.
    /// </summary>
    /// <returns>Array of CSV fields</returns>
    public string[] Read()
    {
        var line = reader.ReadLine();
        var retval = new List<string>();
        if (line == null) 
            return null;
        var state = State.Initial;
        var text = new StringBuilder();
        foreach (var ch in line)
            switch (state)
            {
                case State.Initial:
                    if (ch == '"') 
                        state = State.Quote;
                    else if (ch == ',') 
                        retval.Add(string.Empty);
                    else
                    {
                        text.Append(ch);
                        state = State.Data;
                    }
                    break;
                case State.Data:
                    if (ch == ',')
                    {
                        retval.Add(text.ToString());
                        text.Length = 0;
                        state = State.Initial;
                    }
                    else 
                        text.Append(ch);
                    break;
                case State.Quote:
                    if (ch == '"')
                        state = State.NestedQuote;
                    else 
                        text.Append(ch);
                    break;
                case State.NestedQuote:
                    if (ch == '"')
                    {
                        text.Append('"');
                        state = State.Quote;
                        break;
                    }
                    state = State.Data;
                    goto case State.Data;
            }
        retval.Add(text.ToString());
        return retval.ToArray();
    }
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        reader.Dispose();
    }
To make the matrix (untested):
var data = new List<string[]>();
string[] line;
using(reader = new CsvReader(stream))
  while((line = reader.Read()) != null)
    data.Add(line);
result = data.Select(row => row.Select(cell => int.Parse(cell)).ToArray()).ToArray();
 
    
    - 2,229
- 14
- 15
- 
                    1
- 
                    this is what i want to do: int [][] mat=READ FROM CSV FILE...; my csv file contains int... – aharon Aug 18 '10 at 07:24
- 
                    no, unfortunately carriage returns cause a new row to start : var line = reader.ReadLine(); -- I tried with Excel (2010), and it also did not allow line breaks in quotes, but I don't exactly know the specs – sukru Aug 18 '10 at 07:28
- 
                    ok. but there are couple of things that i don't understand: where is "reader" defiend? 2.is the function CsvReader is the constructor? and what is the parameter that it gets? 3. where should i add the code you added now? – aharon Aug 18 '10 at 07:38
- 
                    The whole code is the definition of a class - named CsvReader :). The reader is actually any input stream. You can have one by "var reader = new FileStream("path")" – sukru Aug 18 '10 at 08:08
- 
                    ok, i made a couple of changes in your code to fit my needs, and it works great! tanks! – aharon Aug 18 '10 at 08:26
- 
                    
There is a text reader in the VisualBasic namespace that can be used in C# and handles even horrible CSV files very well:
Just add a reference to Microsoft.VisualBasic in your project.
 
    
    - 45,739
- 9
- 81
- 112
CSV parsing with regular expressions: http://www.hotblue.com/article0000.aspx?a=0006
To expand the concept with custom separators, see this post: How do I write a regex to match a string that doesn't contain a word?
 
    
    - 1
- 1
 
    
    - 2,911
- 1
- 31
- 47
There are many ways. Starting with a byte by byte reader. It depends on your csv file format (with/without header, line endings, " or '). I've written my own class.
A good reader to start with:
 
    
    - 2,222
- 17
- 20
