c# 逐行写txt_C#for循环逐行写入(C# for loop to write line by line)

本文探讨了如何使用C#的for循环将75行文本写入txt文件,每行包含一个递增的数字。原有的代码使用`System.IO.File.WriteAllText`导致每次只写入最后一行。解决方案是使用`StreamWriter`,它会追加内容而不是覆盖原有内容,从而正确地写入所有74行数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#for循环逐行写入(C# for loop to write line by line)

我试图写一行文本75次,并将数字增加1,直到达到75的条件。 从2开始是有原因的。 这是代码

class WriteTextFile

{

static void Main()

{

string path = "C:\\Users\\Writefile\\test.txt";

string line;

int i = 2;

while (i <= 75 )

{

line = "Error_Flag = 'FOR_IMPORT' and location_type = 'Home' and batch_num = " + i + "\n";

System.IO.File.WriteAllText(@path, line);

i++;

}

}

}

有了这个,它最后只写了一行75。 我希望它用相同的东西写出所有74行,每次只有这个数字上升。 谢谢。

I am trying to write one line of text 75 times and increase the number by 1 till it hits the 75 condition. Starting at 2 for a reason. Here's the code

class WriteTextFile

{

static void Main()

{

string path = "C:\\Users\\Writefile\\test.txt";

string line;

int i = 2;

while (i <= 75 )

{

line = "Error_Flag = 'FOR_IMPORT' and location_type = 'Home' and batch_num = " + i + "\n&#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值