【问题标题】:Streamwriter is only printing the first line of loop to CSV fileStreamwriter 仅将循环的第一行打印到 CSV 文件
【发布时间】:2018-08-04 13:48:59
【问题描述】:

我制作了一个快速程序,因此我可以将 collat​​z 猜想中的数据写入图形,并且我正在尝试将其写入 csv,其中第 1 列中的结果量和第 2 列中的步数。但是由于某种原因,streamwriter 只输出每一行的第一行。在写入 CSV 文件之前如何让流写入器更新?

    static void Main(string[] args)

    {

        int y;


        BigInteger x;

        System.Collections.ArrayList ResultsArray = new System.Collections.ArrayList();

        System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\public\testfolder\writelines2.csv");


        x = BigInteger.Pow(15, 15);
        y = 1;

        var ColumnWriter = string.Format("{0},{1}",x, y);
        do
        {

            if (x % 2 != 0)

            {
                x = (x * 3) + 1;
                Console.WriteLine(x);
                ResultsArray.Add(x);
            }
            else
            {

                x = (x / 2);
                Console.WriteLine(x);
                ResultsArray.Add(x);
            }

           file.WriteLine(ColumnWriter);
            y = y + 1;


        } while (x > 1);

        file.Flush();
        Console.ReadLine();
}
}

}

【问题讨论】:

  • "每一行的第一行"。这甚至意味着什么?
  • 据重新打印循环的第一itteration每一行,而不是下一个中,所以它看起来像这样“437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375 ,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 437893890380859375,1 跨度>

标签: c# streamwriter


【解决方案1】:

您不会在循环内更新字符串变量 ColumnWriter。放

ColumnWriter = string.Format("{0},{1}",x, y);

在该行之前或之后(取决于您想要输出什么/何时)

file.WriteLine(ColumnWriter);

【讨论】:

    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多