File 逐行读取文本文件
我需要逐行读取文本文件中的数据。每行包含一个字符串或一个整数。我想使用StreamReader逐行读取文本文件,并使用StreamWriter将其写入二进制文件。写入二进制文件部分将很容易。逐行读取文本文件部分是我需要帮助的部分。它都内置在StreamReader中:File 逐行读取文本文件,file,text,binaryreader,binarywriter,File,Text,Binaryreader,Binarywriter,我需要逐行读取文本文件中的数据。每行包含一个字符串或一个整数。我想使用StreamReader逐行读取文本文件,并使用StreamWriter将其写入二进制文件。写入二进制文件部分将很容易。逐行读取文本文件部分是我需要帮助的部分。它都内置在StreamReader中: using (var sr = new StreamReader(myFile)) { string line; while ((line = sr.ReadLine()) != null) {
using (var sr = new StreamReader(myFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// line is the text line
}
}
在c语言中,你可以这样做
string loc = "idk/where/ever";
using(var sr = new StreamReader(loc))
using(var sw = new StreamWriter(loc+".tmp"))
{
string line;
while((line=sr.ReadLine())!=null)
{
sw.WriteLine(line);
//edit it however you want
}
}
File.Delete(loc);
File.Move(loc+".tmp",loc);
我尝试了你们的两个建议,但我收到一条错误消息,路径中有非法字符。我只是在读和写纯文本。还有什么我需要做的吗?那条消息意味着你的文件名有问题。尝试调试并查看正在使用的文件名,您可能会发现任意斜杠或其他无效字符。