Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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#_Readline - Fatal编程技术网

C#将多行数据读入单个变量

C#将多行数据读入单个变量,c#,readline,C#,Readline,我已经看过了,但是没有找到一个可以解决我问题的教程。也许我的搜索词不正确。无论如何,我在这里 我接管了一家手工公司,他有大约150名客户。他买了一个为客户制作唱片的程序。我有记录,但他不会卖给我这个节目,因为它是商业性的,他害怕因为卖这样的东西而坐牢。。。无论什么它们写在一个文本文件中。格式似乎是: string name string last_job_description int job_codes string job_address string comments 文本文件如下

我已经看过了,但是没有找到一个可以解决我问题的教程。也许我的搜索词不正确。无论如何,我在这里

我接管了一家手工公司,他有大约150名客户。他买了一个为客户制作唱片的程序。我有记录,但他不会卖给我这个节目,因为它是商业性的,他害怕因为卖这样的东西而坐牢。。。无论什么它们写在一个文本文件中。格式似乎是:

string name
string last_job_description
int    job_codes
string job_address
string comments
文本文件如下所示

*Henderson*
*Cleaned gutters, fed the lawn, added mulch to the front flower beds,
 cut the back yard, edged the side walk, pressure washed the driveway*
*04 34 32 1 18 99 32 22 43 72 11 18*
*123 Anywhere ave*
*Ms.always pays cash no tip. Mr. gives you a check but always tips*

好的。。我的问题是C#我想写一个程序来编辑这些记录,添加新客户,删除一些我可能丢失、移动或死亡的客户。。。但第二条分为两行,有时是3行和4行。它们都以*开头和结尾。那么,如何读取2到4行,并将它们放入最后一个_job_description字符串变量中呢?我可以写课文,我可以读台词,我可以删掉星号。我找不到将多行读入单个变量的方法。

如果要将文件中的所有行读入单个变量,则需要执行此操作

var txt=File.ReadAllText(…您在此处的文件位置…)

或者你可以

var lines=File.ReadAllLines(…您在此处的文件位置)
然后您可以遍历每一行并按原样删除leave的空格


但是根据你的问题,第一行是你真正想要的,你应该读第二行。如果以*开头,则表示此行为
job\u codes
,否则将其附加到pervous read行。对每行执行此操作,直到找到
作业\u代码

using (var fileReader = new StreamReader("someFile"))
        {
            // read some pervous data here

            var last_job_description = fileReader.ReadLine();
            var nextLine = fileReader.ReadLine();
            while (nextLine.StartsWith("*") == false)
            {
                last_job_description += nextLine;
                nextLine = fileReader.ReadLine();
            }
            //you have job_codes in nextline, so parse it now and read next data
        }
或者您甚至可以使用这样一个事实,即每一组数据都是从开始到结束的!!!使用
*
,您可以创建一个函数,该函数读取每个“集合”,并将其作为单行字符串返回,而不管它实际有多少行。假设传递给此函数的读取器变量与上面代码中的StreamReader相同

function string readSingleValue(StreamReader fileReader){
    var result = "";
    do
    {
        result += fileReader.ReadLine().Trim();
    } while (result.EndsWith("*") == false);
    return result;
}

我认为你的问题有点复杂。我想说的是它没有提到一个具体的问题。它至少包含3个不同的编程方面,您需要了解或学习这些方面才能实现您的目标。您必须采取的步骤如下所述

    <> LI>不幸的是,您需要将当前文件视为大字符串类型。
  • 然后您就可以处理这个字符串并分离不同的部分
  • 下一步是定义一个整洁、可靠和健壮的XML文件,它可以保存您的数据
  • 用经过处理的字符串填充
    xml
  • 如果你有一个
    xml
    文件,你可以简单地更新它

没有一种内置的方法可以完全按照您的意愿解析您的文件,因此您必须把手弄脏

下面是一个针对您的具体案例的工作代码示例。我假设您希望将作业代码放在一个数组中,因为它们是由空格分隔的多个整数

每次遇到以“*”开头的行时,我们都会增加一个计数器,告诉我们如何处理您的下一个属性(姓名、上次工作描述等)

每一行都附加到当前属性

显然,我们从每一行的开始和结束处移除星星

string name = String.Empty;
string last_job_description = String.Empty;
int[] job_codes = null;
string job_address = String.Empty;
string comments = String.Empty;

int numberOfPropertiesRead = 0;
var lines = File.ReadAllLines("C:/yourfile.txt");

for (int i = 0; i < lines.Count(); i++)
{
    var line = lines[i];
    bool newProp = line.StartsWith("*");
    bool endOfProp = line.EndsWith("*");

    if (newProp)
    {
        numberOfPropertiesRead++;
        line = line.Substring(1);
    }

    if (endOfProp)
        line = line.Substring(0, line.Length - 1);

    switch (numberOfPropertiesRead)
    {
        case 1: name += line; break;
        case 2: last_job_description += line; break;
        case 3:
            job_codes = line.Split(' ').Select(el => Int32.Parse(el)).ToArray();
            break;
        case 4: job_address += line; break;
        case 5: comments += line; break;
        default:
            throw new ArgumentException("Wow, that's too many properties dude.");
    }
}

Console.WriteLine("name: " + name);
Console.WriteLine("last_job_description: " + last_job_description);
foreach (int job_code in job_codes)
    Console.Write(job_code + " ");
Console.WriteLine();
Console.WriteLine("job_address: " + job_address);
Console.WriteLine("comments: " + comments);
Console.ReadLine();
string name=string.Empty;
string last\u job\u description=string.Empty;
int[]作业代码=null;
string job_address=string.Empty;
字符串注释=string.Empty;
int numberOfPropertiesRead=0;
var lines=File.ReadAllLines(“C:/yourfile.txt”);
对于(int i=0;iInt32.Parse(el)).ToArray();
打破
案例4:作业地址+=行;中断;
案例5:注释+=行;中断;
违约:
抛出新的ArgumentException(“哇,那太多了,伙计。”);
}
}
Console.WriteLine(“名称:”+名称);
Console.WriteLine(“上次作业描述:+上次作业描述”);
foreach(作业代码中的int作业代码)
Console.Write(作业代码+“”);
Console.WriteLine();
Console.WriteLine(“作业地址:+作业地址”);
Console.WriteLine(“注释:+注释”);
Console.ReadLine();
让我们把它做好

首先定义客户模型:

public class Customer
{
    public string Name { get; set; }
    public string LastJobDescription { get; set; }
    public List<int> JobCodes { get; set; }
    public string JobAddress { get; set; }
    public string Comments { get; set; }
}
向最新客户端添加注释:

customers.Last().Comments += " Very generous.";
以Henderson的名字查找客户的第一条记录,并添加所执行工作的代码:

customers.Find(c => c.Name == "Henderson").JobCodes.Add(42);
添加新客户:

customers.RemoveAt(0);
var customer = new Customer
{
    Name = "Chuck Norris",
    LastJobDescription= "Saved the world.",
    JobCodes = new List<int>() { 1 },
    JobAddress = "UN",
    Comments = "Nice guy!"
};
customers.Add(customer);


您可能需要一个图形用户界面。如果是这样,我建议您提出一个新问题,指定您使用的是什么:WinForms、WPF、Web应用程序或其他东西。

您必须分别阅读每一行,然后根据第一行的外观(如果有足够的星号),您必须决定是阅读下一行并附加,还是停止。在.NET中,除了ReadToEnd或类似的方法外,没有任何方法可以让TextReader阅读多行,因此您只需自己阅读这些行,一次一个。或者您可以查看nuget上是否有一个CSV导入库,可以处理数据中的换行符。使用File.readAllLines您可以使用File.ReadAllText将整个文件读入一个
字符串
当文本中出现
'*'
时会发生什么,它是如何转义的?你们应该编写一个关于这方面的教程。我真的找不到太多。微软Visual C#2013 step by step by是一个令人窒息的780多个页面,甚至没有提到修剪/拆分/或任何这些东西。这是非常必要的。
customers.Find(c => c.Name == "Henderson").JobCodes.Add(42);
var customer = new Customer
{
    Name = "Chuck Norris",
    LastJobDescription= "Saved the world.",
    JobCodes = new List<int>() { 1 },
    JobAddress = "UN",
    Comments = "Nice guy!"
};
customers.Add(customer);
var sb = new StringBuilder();
foreach (var customer in customers)
{
    sb.Append('*').Append(customer.Name).Append('*').AppendLine();
    sb.Append('*').Append(customer.LastJobDescription).Append('*').AppendLine();
    sb.Append('*').Append(string.Join(" ", customer.JobCodes)).Append('*').AppendLine();
    sb.Append('*').Append(customer.JobAddress).Append('*').AppendLine();
    sb.Append('*').Append(customer.Comments).Append('*').AppendLine();
}
File.WriteAllText("customers.txt", sb.ToString());