Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解析文本文件?_C#_.net_Streamreader_Textreader - Fatal编程技术网

C# 如何解析文本文件?

C# 如何解析文本文件?,c#,.net,streamreader,textreader,C#,.net,Streamreader,Textreader,基本上,我需要有人来帮助我或向我展示代码,让我从一个名为c1.txt的文件中读取名称和价格 这是我已经拥有的 TextReader c1 = new StreamReader("c1.txt"); if (cse == "c1") { string compc1; compc1 = c1.ReadLine(); Console.WriteLine(compc1);

基本上,我需要有人来帮助我或向我展示代码,让我从一个名为c1.txt的文件中读取名称和价格

这是我已经拥有的

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

另外,如何从文本文档中选择要读取的行也很好

您还没有告诉我们文本文件的格式。我将假设如下:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79
Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79
您也没有指定输出。我将假设如下:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79
Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79
然后,下面将读取这样的文件并生成所需的输出

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)
        );
    }
}
如果分隔符不同,则需要将参数“|”更改为在名为line.Split“|”的字符串实例上调用的方法String.Split

如果您的格式需要不同,那么您需要使用线条

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

如果您有任何问题,请告诉我。

您还没有告诉我们文本文件的格式。我将假设如下:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79
Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79
    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);
        }
您也没有指定输出。我将假设如下:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79
Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79
然后,下面将读取这样的文件并生成所需的输出

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)
        );
    }
}
如果分隔符不同,则需要将参数“|”更改为在名为line.Split“|”的字符串实例上调用的方法String.Split

如果您的格式需要不同,那么您需要使用线条

String.Format("Name = {0}, Price = {1}", name, price)
如果你有任何问题,请告诉我

    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);
        }
它给出了与@Jason方法相同的结果,但我认为这是一个优化版本


它给出的结果与@Jason的方法相同,但我认为这是一个优化版本。

您也可以尝试使用解析帮助器类作为起点,如中所述。

您也可以尝试使用解析帮助器类作为起点,例如。

中描述的,我们需要文本文件中的样本行。家庭作业?文本文件是什么样子的?你能发布一个简短但信息丰富的输入文件摘录,以及你想要的预期输出吗?似乎你不明白你想要的是什么。。。请重新提出这个问题,或者,如果你建议你清洗EnUnGH,请阅读C++手册。如果你不熟悉C或C++,并且寻找快速实现项,试着用VB代替C/C++ +C。建议这个链接可能对你有帮助:我们需要从文本文件中获取一个示例行。文本文件是什么样子的?你能发布一个简短但信息丰富的输入文件摘录,以及你想要的预期输出吗?似乎你不明白你想要的是什么。。。请重新提出这个问题,或者,如果你建议你清洗EnUnGH,请阅读C++手册。如果你不熟悉C或C++,并且寻找快速实现项,尝试使用VB代替C/C++ +C。建议这个链接可能对你有帮助:在一个字符串中创建一个String Bu建器的优化是什么,你根本就不操作,并且两次做一次分割操作而不是一次操作?通常只创建StringBuilder以减少多个附件的成本。在line.Split返回的数组中,您已经得到了两个非常好的字符串。此外,这会一次性将文件读入内存,这也会造成读取时间问题以及潜在的内存问题。在一个您根本不需要操作的字符串上创建StringBuilder的优化是什么,而拆分操作是两次而不是一次?通常只创建StringBuilder以减少多个附件的成本。在line.Split返回的数组中已经有两个非常好的字符串。此外,这会一次将文件读入内存,这也会造成读取时间问题以及潜在的内存问题。