Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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#控制台中从txt读取和写入一行中的特定单词_C#_Console Application - Fatal编程技术网

如何在c#控制台中从txt读取和写入一行中的特定单词

如何在c#控制台中从txt读取和写入一行中的特定单词,c#,console-application,C#,Console Application,我需要得到的产品代码,价格和股票的txt文件中包含的计算 //product code;product name;price;stock; c01;cappuccino;3500;12; c02;mocaccino;4000;15; c03;black coffe;3000;10; c04;chocolate milk;5000;19; c05;vanilla milk;5000;12; c06;strawberry milk;5000;13; c07;coke;3000;13; c08;fan

我需要得到的产品代码,价格和股票的txt文件中包含的计算

//product code;product name;price;stock;
c01;cappuccino;3500;12;
c02;mocaccino;4000;15;
c03;black coffe;3000;10;
c04;chocolate milk;5000;19;
c05;vanilla milk;5000;12;
c06;strawberry milk;5000;13;
c07;coke;3000;13;
c08;fanta;3000;15;
c09;sprite;3000;9;
c10;orange juice;4500;10;
c11;apple juice;4500;9;
c12;mango juice;4500;18;
我试过了

if(line.Contains(""))
    FileStream fs = new FileStream("product.txt", FileMode.OpenOrCreate, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);

    sr.BaseStream.Seek(0, SeekOrigin.Begin);
    string str = sr.ReadLine();
    while (str != null)
    {
        Console.WriteLine("{0}", str);
        str = sr.ReadLine();
    }
    sr.Close();
    fs.Close();
但是line.contains中的包含错误,请改为使用红色下划线,我是否缺少为此使用名称空间

我也试过了

if(line.Contains(""))
    FileStream fs = new FileStream("product.txt", FileMode.OpenOrCreate, FileAccess.Read);
    StreamReader sr = new StreamReader(fs);

    sr.BaseStream.Seek(0, SeekOrigin.Begin);
    string str = sr.ReadLine();
    while (str != null)
    {
        Console.WriteLine("{0}", str);
        str = sr.ReadLine();
    }
    sr.Close();
    fs.Close();

按照惯例,获取一个单词,但它会返回txt中的所有内容,如果您想迭代这些值,可以如下所示:

var streamReader = new StreamReader(new FileStream("c:\\file.txt"));

while(!streamReader.EndOfStream)
{
var line = streamReader.ReadLine()
var values = line.Split(';');
for(var i = 0; i < line.Length; i++)
    Console.WriteLine(values[i]); //example usage
}
var streamReader=newstreamreader(newfilestream(“c:\\file.txt”);
而(!streamReader.EndOfStream)
{
var line=streamReader.ReadLine()
变量值=行分割(“;”);
对于(变量i=0;i
使用以下类和代码,您将能够提取进行计算和其他操作所需的数据

它基本上是逐行读取文件并将其解析为对象。
通过检查
“//”
跳过第一行

希望这适合你的需要

产品类别

public class Product
{
    public string Code { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Stock { get; set; }
}
文件解析

var products = new List<Product>();

using (var fileStream = new FileStream("product.txt", FileMode.OpenOrCreate, FileAccess.Read))
using (var streamReader = new StreamReader(fileStream))
{
    string line;
    while (!String.IsNullOrWhiteSpace((line = streamReader.ReadLine())))
    {
        if (!line.StartsWith("//"))
        {
            var lineSplit = line.Split(';');
            products.Add(new Product
            {
                Code = lineSplit[0],
                Name = lineSplit[1],
                Price = Decimal.Parse(lineSplit[2]),
                Stock = Int32.Parse(lineSplit[3])
            });
        }
    }
}
var产品=新列表();
使用(var fileStream=newfilestream(“product.txt”、FileMode.OpenOrCreate、FileAccess.Read))
使用(var streamReader=newstreamreader(fileStream))
{
弦线;
而(!String.IsNullOrWhiteSpace((line=streamReader.ReadLine()))
{
如果(!line.StartsWith(“/”)
{
var lineSplit=line.Split(“;”);
产品。添加(新产品)
{
代码=行拆分[0],
Name=lineSplit[1],
Price=Decimal.Parse(lineSplit[2]),
Stock=Int32.Parse(lineSplit[3])
});
}
}
}

“但是line.contains中的行出现了错误,请改为使用红色下划线,我是否缺少…”-Pro提示:如果将鼠标移到红色下划线/squiggle上并在那里停留片刻,Visual Studio将显示一个工具提示,说明我在尝试
Console.WriteLine(linesplit[0])时遇到的问题它返回所有产品代码,如何只获取单个产品代码?简短、甜美、易懂。尽管如此,我还是更喜欢它在F#中的样子(见)