Hello, am a newbie in c#.. i have a question, how do you use read data from file and use it?

my data file is something like this:

Name amount1 amount2

johen 1234   231411

i have this code so far:

using System;
using System.Data;
using System.IO;



class Class1
{
    static void Main(string[] args)
    {
        string value;
        StreamReader sr = new StreamReader("Aqua.txt");
        value=sr.ReadToEnd();
        Console.WriteLine(""+value);

        sr.Close();
        Console.Read();
    }
}

but my code so far only read it but how do i use the value in it?
thx for any help and sorry if my english is bad...

Recommended Answers

All 4 Replies

Take a look as this and this they might be of some help

Hi,
I would suggest you to use ReadLine() method, not ReadToEnd(). This way you avoid some additional code, which would be needed if you would use ReadToEnd() method.
So after using ReadLine() method, you get a string of text line by line. Each line will habe the shape of you:

"Name amount1 amount2"

What you can do is to split string (by whitespace), and:
1. put into a stirng array and all add to generic list
2. create a new class and assign data to properties of this class and add to generic List<T>
-------------------------------------------------

1.

List<string[]> list  = new List<string[]>();
StreamReader sr = new StreamReader("Aqua.txt");
string line;
while((line = sr.ReadLine()) != null)
{
    string[]array = line.Spit(' ');
    list.Add(array); 
}

//when retreiving data loop over list and access to the vlaues by settting indexes:
foreach(string item in list)
{
    string name = item[0];
    string no1 = item[1];   
    string no1 = item[2];
    //you can convert to int if oyu like:
    int intNo1 = int.Parse(item[0]);
}
List<string[]> list  = new List<string[]>();

hmm...seems like it does not recognize list...do i need to include something in the system part?

using System;
using System.Data;
using System.IO;

by the way im using vb 2008

never mind ..got it...thx u guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.