Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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#_File - Fatal编程技术网

如何在c#中选择和重写文本文件的特定部分?

如何在c#中选择和重写文本文件的特定部分?,c#,file,C#,File,我有一个文本文件,如下所示: 22.3 181010 0 0 0 2 3 2 15900120 17900117 我将此文本文件解读为: int counter = 0; string line; StreamReader file = new StreamReader("../../normal.txt"); while ((line = file.ReadLine()) != null) { Console.WriteLine(line); counter++; } 之后,我想

我有一个文本文件,如下所示:

22.3
181010
0 0 0 2 3 2
15900120
17900117

我将此文本文件解读为:

int counter = 0;
string line;
StreamReader file = new StreamReader("../../normal.txt");
while ((line = file.ReadLine()) != null)
{
   Console.WriteLine(line);
   counter++;
}
之后,我想删除前两行。除此之外,选择其余行中的第一个和第三个字符,并在已阅读的文本下重写它们。因此,最终输出将是:

22.3
181010
0 0 0 2 3 2
15900120
17900117
0 0
15 0
170


我怎样才能做到这一点呢?

像这样的事情怎么样:

List<string> lineList = new List<string>();
while ((line = file.ReadLine()) != null)
{
    Console.WriteLine(line);
    lineList.add(line);
    counter++;
}

for(int i = 2; i < lineList.Count; i++) {
    string[] split = lineList[i].Split(new char[] {' '});
    Console.WriteLine(string.Format("{0} {1}", split[0], split[2]));
}
List lineList=new List();
而((line=file.ReadLine())!=null)
{
控制台写入线(行);
lineList.add(行);
计数器++;
}
对于(int i=2;i
像这样的东西怎么样:

List<string> lineList = new List<string>();
while ((line = file.ReadLine()) != null)
{
    Console.WriteLine(line);
    lineList.add(line);
    counter++;
}

for(int i = 2; i < lineList.Count; i++) {
    string[] split = lineList[i].Split(new char[] {' '});
    Console.WriteLine(string.Format("{0} {1}", split[0], split[2]));
}
List lineList=new List();
而((line=file.ReadLine())!=null)
{
控制台写入线(行);
lineList.add(行);
计数器++;
}
对于(int i=2;i
以获得指定的输出

列表数据=新列表();
列表行=File.ReadAllLines(“../../normal.txt”).ToList();
foreach(行中的字符串项。跳过(2))
{
data=item.Split(新字符[]{''}).ToList();
Add(string.Format(“{0}{1}”),data[0],data[2]);
}
以获得指定的输出

列表数据=新列表();
列表行=File.ReadAllLines(“../../normal.txt”).ToList();
foreach(行中的字符串项。跳过(2))
{
data=item.Split(新字符[]{''}).ToList();
Add(string.Format(“{0}{1}”),data[0],data[2]);
}
var existingLines=File.ReadAllLines(“../../normal.txt”);
var newLines=新列表();
var appendedLines=新列表();
对于(变量i=2;i
var existingLines=File.ReadAllLines(“../../normal.txt”);
var newLines=新列表();
var appendedLines=新列表();
对于(变量i=2;i
从文件中获取输入。删除前两行。然后,下一行将是:0 0 0 0 2 3 2。对于所有剩余的行,仅获取第一行和第三行数字,在本例中为0 0,依此类推。最后将所有内容添加到文件的原始内容中。至少对我来说似乎没问题。哦,我现在看到了,很抱歉t、 把它看错了,你就明白了upvote@MichaelFaisst,您的建议在console视图中运行良好。此外,如果我想将此输出重写为文本文件,如何执行此操作?不必将新生成的行打印到console,您可以将其添加到新的字符串列表中。在for循环之后,您可以使用file.AppendAllLines函数将所有新行追加到现有文件中。从文件中获取输入。删除前两行。然后,下一行将是:0 0 0 2 3 2。对于所有剩余行,仅获取第一行和第三行数字,在本例中为0 0,依此类推。最后将所有内容添加到文件的原始内容中。如图所示至少对我来说是这样。哦,我现在看到了,对不起,看错了。你明白了upvote@MichaelFaisst,您的建议在console视图中运行良好。除此之外,如果我想将此输出重写为文本文件,如何执行此操作?您可以将新生成的行添加到新的字符串列表中,而不是打印到console对于for循环,您可以使用File.AppendAllLines函数将所有新行追加到现有文件。将拆分数组转换为列表有什么好处吗?我个人更喜欢列表而不是数组,因为它是可消耗的。好吧,但在这个特定示例中,它没有任何好处……我只是想知道^^转换y有什么好处吗我们将数组拆分为列表?我个人更喜欢列表而不是数组,因为它是可消耗的。好吧,但在这个特定示例中,它没有任何好处……我只是想知道^^
     var existingLines = File.ReadAllLines("../../normal.txt");
     var newLines = new List<string>();
     var appendedLines = new List<string>();

     for (var i = 2; i < existingLines.Length; i++)
     {
            // add a line
            newLines.Add(existingLines[i]);

            // add first and third character to the line
            var split = existingLines[i].Split(' ');
            appendedLines.Add(string.Format("{0} {1}", split[0], split[2]));
     }
     newLines.AddRange(appendedLines);

     File.WriteAllLines("../../newText.txt", newLines);