从 .txt 文件读取双打并将它们复制到新的 .txt 文件 (Streamreader/Streamwriter)
Reading doubles from a .txt file and copying them to a new .txt file (Streamreader/Streamwriter)
static void Main(string[] args)
{
Console.Write("Enter the name of the file to open: ");
string input = Console.ReadLine();
Console.Write("Enter the name of the output file: ");
string output = Console.ReadLine();
var InFile = new StreamReader(input);
int TotalD = 0;
StreamWriter OutFile = new StreamWriter(output);
Console.WriteLine();
try
{
using (InFile)
{
double DInput = 0;
while (DInput == double.Parse(InFile.ReadLine()))
{
while (!InFile.EndOfStream)
{
DInput = double.Parse(InFile.ReadLine());
double DOutput = DInput * 2;
InFile.Close();
using (OutFile)
{
OutFile.WriteLine(DOutput);
}
}
InFile.Close();
}
}
using (StreamReader r = new StreamReader(input))
{
TotalD = 0;
while (r.ReadLine() != null) { TotalD++; }
}
}
catch (IOException Except)
{
Console.WriteLine(Except.Message);
}
Console.WriteLine("There were {0} doubles copied to {1}.", TotalD, output);
Console.WriteLine();
Console.Write("Press <enter> to exit the program: ");
Console.Read();
}
好的,所以我之前问过这个问题,但问题是我不知道 input.txt 文件必须手动编写,而不是像以前那样生成。当前版本也可以在没有错误消息的情况下运行。现在唯一的问题是 output.txt 完全空白。任何帮助将不胜感激!
这个项目的想法是阅读 input.txt:
的双打
1.5
2.3
3
4.7
5
并将它们写入 output.txt 仅次 2:
3.0
4.6
6
9.4
10
string input = @"C:\temp\testinput.txt";
string output = @"C:\temp\testoutput.txt";
using (var reader = new StreamReader(input))
{
using (var writer = new StreamWriter(output))
{
var line = reader.ReadLine();
while (line != null)
{
var value = float.Parse(line);
var result = value * 2;
writer.WriteLine(result);
line = reader.ReadLine();
}
}
}
将输出格式化为您想要的格式。
static void Main(string[] args)
{
Console.Write("Enter the name of the file to open: ");
string input = Console.ReadLine();
Console.Write("Enter the name of the output file: ");
string output = Console.ReadLine();
var InFile = new StreamReader(input);
int TotalD = 0;
StreamWriter OutFile = new StreamWriter(output);
Console.WriteLine();
try
{
using (InFile)
{
double DInput = 0;
while (DInput == double.Parse(InFile.ReadLine()))
{
while (!InFile.EndOfStream)
{
DInput = double.Parse(InFile.ReadLine());
double DOutput = DInput * 2;
InFile.Close();
using (OutFile)
{
OutFile.WriteLine(DOutput);
}
}
InFile.Close();
}
}
using (StreamReader r = new StreamReader(input))
{
TotalD = 0;
while (r.ReadLine() != null) { TotalD++; }
}
}
catch (IOException Except)
{
Console.WriteLine(Except.Message);
}
Console.WriteLine("There were {0} doubles copied to {1}.", TotalD, output);
Console.WriteLine();
Console.Write("Press <enter> to exit the program: ");
Console.Read();
}
好的,所以我之前问过这个问题,但问题是我不知道 input.txt 文件必须手动编写,而不是像以前那样生成。当前版本也可以在没有错误消息的情况下运行。现在唯一的问题是 output.txt 完全空白。任何帮助将不胜感激!
这个项目的想法是阅读 input.txt:
的双打1.5
2.3
3
4.7
5
并将它们写入 output.txt 仅次 2:
3.0
4.6
6
9.4
10
string input = @"C:\temp\testinput.txt";
string output = @"C:\temp\testoutput.txt";
using (var reader = new StreamReader(input))
{
using (var writer = new StreamWriter(output))
{
var line = reader.ReadLine();
while (line != null)
{
var value = float.Parse(line);
var result = value * 2;
writer.WriteLine(result);
line = reader.ReadLine();
}
}
}
将输出格式化为您想要的格式。