1

This question has been asked several times with different inputs so I thought of reposting it with my requirement.

I have a CSV file which contents string fields in the way given below.

idnum,name1, name2,groupid

idnum,name1, name2,groupid

idnum,name1, name2,groupid

example

s001,sahil,payap,gid0

s002,Amir,Khan,gid02

d003,hrithik,roshan,gid03

I have two dimensional string array. I want to read row by row to my two dimensional array. When it read it should be like this

arr[0][0]=s001

arr[0][1]=name1

arr[0][2]=name2

arr[0][3]=gid01

arr[1][0]=s002

arr[1][1]=Amir

arr[1][2]=Khan

arr[1][3]=gid04

there are 40 records in a file and it should read till the end of the file. I need to implement this in C#

Any code sample or any explanation would be great help.

I have no knowledge in csv file handling so please don't ask what did you try, at least if you could give me a code sample for reading just one string for a variable it would be a great help. And please don't ask to go for another solution. Thanks.

leppie
  • 115,091
  • 17
  • 196
  • 297
user3572467
  • 59
  • 1
  • 1
  • 8

3 Answers3

4

The simplest way to read a csv file in the way you suggest is probably:

var rows = File.ReadAllLines("myfile.csv").Select(l => l.Split(',').ToArray()).ToArray();

Then:

Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003
Ashigore
  • 4,618
  • 1
  • 19
  • 39
3

The file would have to be read in line-wise. Each line would have to be separated using String.Split. Then the resulting strings would have to be trimmed using Trim, and finally would have to be written into the respective columns of the current row. However I totally second the comments above; more convenient would be to use some class or struct called Person and then to parse into a List<Person>.

The reading could be done as follows:

String line = String.Empty;
System.IO.StreamReader file =  new System.IO.StreamReader("c:\\file.txt");
while((line = file.ReadLine()) != null)
{
    String[] parts_of_line = line.Split(',')
    for ( int i = 0; i < parts_of_line.Length; i++ )
        parts_of_line[i] = parts_of_line[i].Trim();

    // do with the parts of the line whatever you like

}
Codor
  • 17,447
  • 9
  • 29
  • 56
  • can you show me the way just to do at least for a single line? Or just read a single string line in a csv file to a variable.. – user3572467 Apr 25 '14 at 12:08
-1

You can do that using the CsvHelper library:

const string Csv = @"s001,sahil,payap,gid0
s002,Amir,Khan,gid02
d003,hrithik,roshan,gid03";

var rows = new List<string[]>();
string[] row;

using (var stringReader = new StringReader(Csv))
using (var parser = new CsvParser(stringReader))
while ((row = parser.Read()) != null)
{
    rows.Add(row);
}

Console.WriteLine(rows[0][0]); // Will output s001
Console.WriteLine(rows[0][1]); // Will output sahil
Console.WriteLine(rows[0][2]); // Will output payap
Console.WriteLine(rows[0][3]); // Will output gid0
Console.WriteLine(rows[1][0]); // Will output s002
Console.WriteLine(rows[2][0]); // Will output d003

For a working example, check out this .NET fiddle: http://dotnetfiddle.net/PLPXo8

If you want to read directly from file, you can do this:

var rows = new List<string[]>();
string[] row;

using (var parser = new CsvParser(File.OpenText(@"c:\test.csv")))
while ((row = parser.Read()) != null)
{
      rows.Add(row);
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • Thanks but I would like a little bit explanation, What is that csv string for? is that the line for ? //This line //const string Csv = @"s001,sahil,payap,gid0 s002,Amir,Khan,gid02 d003,hrithik,roshan,gid03"; i want to open file and read row by row, so is this the line to locate the file.? confused with this line too? var rows = new List(); why use array in <>? I plan to do the out puit in a different way so I don't need the way to out put the array contents but I want clear explanations for how to read in to array. Any sites to refer as a tutorial? in CSV file reading? – user3572467 Apr 25 '14 at 12:04
  • @user3572467 SO isn't here to teach you how to code. You ask a specific question and get specific answers. – Ashigore Apr 25 '14 at 12:09
  • @Ashigore yes sir true but is that's the case I would like to have little bit explanation. – user3572467 Apr 25 '14 at 12:18
  • Well, the `Csv` variable is only there for illustration purposes. I have updated the sample to show how to directly read from a file. – Erik Schierboom Apr 25 '14 at 12:22