03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ow. This is similar <strong>to</strong> a spreadsheet where the row is represented by a line, and the<br />

columns are delimited by commas. For example:<br />

Fred,20<br />

Bert,10<br />

Colin,15<br />

Berty,26<br />

Freddy,22<br />

We can then modify the reading method <strong>to</strong> parse the input line from<br />

the CSV file:<br />

do<br />

{<br />

text=reader.ReadLine();<br />

if (text==null) break;<br />

string[] str =text.Split(',');<br />

name[i]=str[0];<br />

age[i]=Convert.ToInt32(str[1]);<br />

i++;<br />

} while (text != null);<br />

The complete listing is given in Program 3.6.<br />

Program 3.6: Program3_6_ArrayGenerateDataWithCSVParse<br />

using System;<br />

using System.IO;<br />

namespace ConsoleApplication2<br />

{<br />

class ArrayExample02<br />

{<br />

const int ARRAYSIZE=100;<br />

static void fillData(string[] name, int[] age)<br />

{<br />

int i=0;<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

}<br />

FileInfo theSourceFile = new FileInfo("..\\..\\test.csv");<br />

StreamReader reader = theSourceFile.OpenText();<br />

string text;<br />

do<br />

{<br />

text=reader.ReadLine();<br />

if (text==null) break;<br />

string[] str =text.Split(',');<br />

name[i]=str[0];<br />

age[i]=Convert.ToInt32(str[1]);<br />

i++;<br />

} while (text != null);<br />

reader.Close();<br />

Agilent .NET Course: Arrays and Collections 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!