我有一个函数,它读取整个文件,然后是一个单独的函数,它将内容打印到文本文档,但它只在第一个循环中打印。
我的代码是:
private void LineModule ( StreamReader reader,StreamWriter writer1 = null) //Interpretes line objects in the DXF file
{
// creates a StreamWriter ready to write data to a file
using (StreamWriter writer = new StreamWriter("blahblah.txt"))
{
string line1, line2;
line1 = "0";
line2 = "0";
double x1= 0;
double y1 = 0;
double x2= 0;
double y2 = 0;
do
{
GetLineCouple(reader, out line1, out line2);
if (line2 != "EOF")
{ }
if (line1 == "10")
{
x1 = Convert.ToDouble(line2);
if (x1 > XMax)
XMax = x1;
if (x1 < XMin)
XMin = x1;
}
if (line1 == "20")
{
y1 = Convert.ToDouble(line2);
if (y1 > YMax)
YMax = y1;
if (y1 < YMin)
YMin = y1;
string xyz1 = (x1 + "," + line2 + ",0.0");
xyz1 = xyz1.TrimEnd();
writer.WriteLine(xyz1);
}
if (line1 == "11")
{
x2 = Convert.ToDouble(line2);
if (x2 > XMax)
XMax = x2;
if (x2 < XMin)
XMin = x2;
}
if (line1 == "21")
{
y2 = Convert.ToDouble(line2);
if (y2 > YMax)
YMax = y2;
if (y2 < YMin)
YMin = y2;
string xyz2 = (x2 + "," + line2 + ",0.0\n");
xyz2 = xyz2.TrimEnd();
writer.WriteLine(xyz2);
}
}
while (line1 != "21");
第一个循环正是我想要的,但我希望它打印出要归档的所有循环。
getlinecouple函数:
private void GetLineCouple(StreamReader theReader, out string line1, out string line2, StreamWriter writer1 = null) //this method is used to iterate through the text file and assign values to line1 and line2
{
System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
string decimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;
{
line1 = line2 = "";
if (theReader == null)
return;
line1 = theReader.ReadLine();
if (line1 != null)
{
line1 = line1.Trim();
line1 = line1.Replace('.', decimalSeparator[0]);
// writer1.WriteLine(line1);
}
line2 = theReader.ReadLine();
if (line2 != null)
{
line2 = line2.Trim();
line2 = line2.Replace('.', decimalSeparator[0]);
//writer1.WriteLine(line2);
}
}
...
}
答案 0 :(得分:0)
调试GetLineCouple,并观察line1值。我猜它在第一次迭代期间设置为21 in。当它进入行!= 21时它返回false并退出while循环。
答案 1 :(得分:0)
第一次通过循环后,line1
“21”的值是否为“
您应该运行调试器并逐步执行代码。这样,您可以在每个步骤中查看变量值,并可以检查退出条件。
我重构了你的LineModule方法,以提取一些重复的代码并解决一些问题:
private void LineModule ( StreamReader reader,StreamWriter writer1 = null) //Interpretes line objects in the DXF file
{
// creates a StreamWriter ready to write data to a file
using (StreamWriter writer = new StreamWriter("blahblah.txt"))
{
string line1 = "0", line2 = "0";
double x1= 0, x2 = 0, y1 = 0, y2 = 0;
do
{
GetLineCouple(reader, out line1, out line2);
switch (line1)
{
case "10":
x1 = Convert.ToDouble(line2);
SetMaxMin(x1, ref XMax, ref XMin);
break;
case "20":
y1 = Convert.ToDouble(line2);
SetMaxMin(y1, ref YMax, ref YMin);
PrintXYX(x1, line2, writer);
break;
case "11":
x2 = Convert.ToDouble(line2);
SetMaxMin(x2, ref XMax, ref XMin);
break;
case "21":
y2 = Convert.ToDouble(line2);
SetMaxMin(y2, ref YMax, ref YMin);
PrintXYZ(x2, line2, writer);
break;
default:
// deal with other values here
break;
}
}
while (line1 != "21");
}
}
private static void PrintXYZ(double value, string lineValue, StreamWriter writer)
{
string xyz = (value + "," + lineValue + ",0.0");
writer.WriteLine(xyz);
}
private static void SetMaxMin(double value, ref double max, ref double min)
{
if (value > max)
{
max = value
}
if (value < min)
{
min = value;
}
}
答案 2 :(得分:0)
感谢您的所有答案。
我通过将while!=“21”更改为while!=“EOF”来解决问题,然后循环遍历整个文件。
现在唯一的问题是程序崩溃了,但确实输出了正确的信息。
再次感谢您