C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
read a text file and perform some calcuation Posted by kristian86 on 11 Dec 2008 at 4:24 PM
hi

I have a problem on how to read text from file and perform operations on it for example

i have this text file that include

//name-//sex---------//birth //m1//m2//m3

fofo, male, 1986, 67, 68, 69
momo, male, 1986, 99, 98, 100
Habs, female, 1988, 99, 100, 87
toto, male, 1989, 67, 68, 69
lolo, female, 1990, 89, 80, 87
soso, female, 1988, 99, 100, 83


I want to read it and get the following

avg m1 : //average of all m1
avg m2 : //average of all m2
avg m3 : //average of all m3
avg Male M1 : //average of all m1 for males only
avg Male M2 : //average of all m2 for males only
avg Male M3 : //average of all m3 for males only
avg Female M1 : //average of all m1 for females only
avg Female M2 : //average of all m2 for females only
avg Female M3 : //average of all m3 for females only

Its allwoed to use regular experssion and the system.IO

can any one help

Report
Re: read a text file and perform some calcuation Posted by sagron on 24 Dec 2008 at 3:36 PM
Hey, assuming the text is always formatted that way you can use the following(using System.IO, System.Text.RegularExpressions, System.Collections.Generic) though you can do it while reading instead of sticking into a collection.
Code:
List<Person> people = new List<Person>();
StreamReader test = new StreamReader(new FileStream("C:\\test.txt", FileMode.OpenOrCreate, FileAccess.Read));
while (test.Peek() != -1)
{
    Regex reg = new Regex(", ");
    object[] details = reg.Split(test.ReadLine());
    Person newPerson = new Person((String)details[0], (String)details[1], int.Parse((string)details[2]), int.Parse((string)details[3]),
        int.Parse((string)details[4]), int.Parse((string)details[5]));
    people.Add(newPerson);
}
foreach (Person pers in people)
{
    System.Console.WriteLine(pers.name);
}

and the Person Class is:

public String name, sex;
public int birth, m1, m2, m3;

public Person(String name, String sex, int birth, int m1, int m2, int m3)
{
    this.name = name;
    this.sex = sex;
    this.birth = birth;
    this.m1 = m1;
    this.m2 = m2;
    this.m3 = m3;
}


The storage will allow you to do things with it after like sorting, searching, etc. In that last foreach you do w/e calculations you want - just have a variable(or several) initialized outside the loop to keep track.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.