1

Basically I need someone to help me or show me the code that will allow me to read a name and a price from a file i have called c1.txt.

This is what i already have.

    TextReader c1 = new StreamReader("c1.txt");
        if (cse == "c1")
        {
            string compc1;
            compc1 = c1.ReadLine();
            Console.WriteLine(compc1);
            Console.WriteLine();
            compcase = compc1;
            compcasecost = 89.99;
        }

also how to select a line to read from a text document would be great.

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 3
    We will need a sample line from the text file. – Jader Dias Jan 14 '10 at 22:11
  • 1
    Homework? What does the text file look like? – Seth Jan 14 '10 at 22:11
  • Can you post a short but informative extract of your input file, and the expected output that you desire? – Mark Byers Jan 14 '10 at 22:12
  • Seems that you don't understand you are asking for ... Please, reformulate the question or, if you suggest that you clean enoungh, read C++ manual. If you don't familiar with C or C++ and look for fast implementation term, try to use VB instead of C/C++/C#. Suggest that this link may be helpful for you: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/33854a80-1db0-4d9b-aacc-68032f888db7 – ThinkJet Jan 14 '10 at 22:22

3 Answers3

10

You haven't told us the format of the text file. I am going to assume the following:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79

You also didn't specify the output. I am going to assume the following:

Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79

Then the following will read such a file and produce the desired output.

using(TextReader tr = new StreamReader("c1.txt")) {
    string line;
    while((line = tr.ReadLine()) != null) {
        string[] fields = line.Split('|');
        string name = fields[0];
        decimal price = Decimal.Parse(fields[1]);
        Console.WriteLine(
            String.Format("Name = {0}, Price = {1}", name, price)
        );
    }
}

If your separator is different then you need to change the parameter '|' to the method String.Split (invoked on the instance of String named line as line.Split('|')).

If your format needs to be different then you need to play with the line

String.Format("Name = {0}, Price = {1}", name, price)

Let me know if you have any questions.

jason
  • 236,483
  • 35
  • 423
  • 525
0
    static void ReadText()
    {
        //open the file, read it, put each line into an array of strings
        //and then close the file
        string[] text = File.ReadAllLines("c1.txt");

        //use StringBuilder instead of string to optimize performance
        StringBuilder name = null;
        StringBuilder price = null;
        foreach (string line in text)
        {
            //get the name of the product (the string before the separator "," )
            name = new StringBuilder((line.Split(','))[0]);
            //get the Price (the string after the separator "," )
            price = new StringBuilder((line.Split(','))[1]);

            //finally format and display the result in the Console
            Console.WriteLine("Name = {0}, Price = {1}", name, price);
        }

It gives the same results as @Jason's method, but I think this is an optimized version.

SteveC
  • 15,808
  • 23
  • 102
  • 173
iChaib
  • 469
  • 4
  • 10
  • 17
  • 2
    What is the optimization in creating a StringBuilder on a string that you're not manipulating at all, and doing the split operation twice instead of once? You usually only create StringBuilder reduce the cost of multiple appends. And you already get two perfectly good strings in the array returned by line.Split. – JasonTrue Jan 14 '10 at 23:24
  • 2
    Further, this reads the file into memory all at once which could also create issues (time to read plus potential memory issues). – jason Jan 15 '10 at 00:20
0

You can also try using a parsing helper class as a starting point, such as the one described at http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466