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
答案 0 :(得分:1)
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();
}
}
}
将输出格式化为文件,使其看起来像您想要的那样。