0

这可能不是一个很好的问题,但我正在尝试将两个文件输出到一个 CSV 文件。我正在使用一个while循环来读取这两个文件,然后我正在调用一个将其全部打印出来的方法,但是当它打印出来时,我并没有得到这两个文件中的所有信息。这是我的while循环代码:

private void mergeBtn_Click(object sender, EventArgs e)
{




    if (System.IO.File.Exists("OUTPUT.csv"))
    {
        System.IO.File.Delete("OUTPUT.csv");
        output = new System.IO.StreamWriter("OUTPUT.csv");
    }

    else
    {
        output = new System.IO.StreamWriter("OUTPUT.csv");
    }


    String[] head = File.ReadAllLines(textBox3.Text); //header file array
    foreach (string h in head)
    {
        output.WriteLine(QuoteString(h));
    }


    input = new StreamReader(textBox1.Text);
    input2 = new StreamReader(textBox2.Text);


    String line, templeaseName2, tempfieldName2, tempreservoir2, tempoperator2, tempcounty2, tempstate2, tempmajo2, tempReserveCat2, tempDiscountRate2, tempnetOil2Int2, tempnetGas2Int2, tempworkingInt2
        , tempgrossWells2, tempultOil2, tempultGas2, tempgrossOil2, tempgrossNGL2, tempgrossGas2, tempnetOil2, tempnetGas2, tempnetNGL2, temprevToInt2, tempoperExpense2, temptotInvest2, temprevOil2
        , temprevGas2, tempoperatingProfit2, temprevNGL2, tempDiscNetInc, tempseqNum2, tempwellID2, tempincASN2, templifeYears2, tempownQual2, tempprodTax2, tempAdValorem2;

    while ((line = input2.ReadLine()) != null || (line = input.ReadLine()) != null)
    {
        templeaseName2 = line.Substring(0, 33);
        tempfieldName2 = line.Substring(33, 33);
        tempreservoir2 = line.Substring(66, 21);
        tempoperator2 = line.Substring(87, 25);
        tempcounty2 = line.Substring(112, 21);
        tempstate2 = line.Substring(133, 2);
        tempmajo2 = line.Substring(136, 3);
        tempReserveCat2 = line.Substring(141, 4);
        tempDiscountRate2 = line.Substring(146, 6);
        tempnetOil2Int2 = line.Substring(152, 10);
        tempnetGas2Int2 = line.Substring(162, 10);
        tempworkingInt2 = line.Substring(172, 10);
        tempgrossWells2 = line.Substring(182, 6);
        tempultOil2 = line.Substring(188, 14);
        tempultGas2 = line.Substring(202, 14);
        tempgrossOil2 = line.Substring(216, 14);
        tempgrossNGL2 = line.Substring(230, 14);
        tempgrossGas2 = line.Substring(244, 14);
        tempnetOil2 = line.Substring(258, 14);
        tempnetGas2 = line.Substring(272, 14);
        tempnetNGL2 = line.Substring(286, 14);
        temprevToInt2 = line.Substring(300, 14);
        tempoperExpense2 = line.Substring(314, 14);
        temptotInvest2 = line.Substring(328, 14);
        temprevOil2 = line.Substring(342, 14);
        temprevGas2 = line.Substring(356, 14);
        tempoperatingProfit2 = line.Substring(370, 14);
        temprevNGL2 = line.Substring(384, 14);
        tempDiscNetInc = line.Substring(398, 14);
        tempseqNum2 = line.Substring(412, 6);
        tempwellID2 = line.Substring(418, 6);
        tempincASN2 = line.Substring(425, 6);
        templifeYears2 = line.Substring(432, 6);
        tempownQual2 = line.Substring(439, 9);
        tempprodTax2 = line.Substring(449, 13);
        tempAdValorem2 = line.Substring(462, 14);

        add(templeaseName2, tempfieldName2, tempreservoir2, tempoperator2, tempcounty2, tempstate2, tempmajo2, tempReserveCat2, tempDiscountRate2, tempnetOil2Int2, tempnetGas2Int2, tempworkingInt2,
            tempgrossWells2, tempultOil2, tempultGas2, tempgrossOil2, tempgrossNGL2, tempgrossGas2, tempnetOil2, tempnetGas2, tempnetNGL2, temprevToInt2, tempoperExpense2, temptotInvest2, temprevOil2,
            temprevGas2, tempoperatingProfit2, temprevNGL2, tempDiscNetInc, tempseqNum2, tempwellID2, tempincASN2, templifeYears2, tempownQual2, tempprodTax2, tempAdValorem2);

    }

    printAll();
    input.Close();
    input2.Close();
    output.Close();
}

我真的不确定为什么它没有输出文件中的所有信息,任何帮助将不胜感激。我会把我的printall();方法放在这里,以防万一这也是问题所在:

public void printAll()
{

    output.WriteLine();
    output.WriteLine();
    output.WriteLine();

    output.WriteLine("Lease Name, Field Name, Reservoir, Operator, County, ST, Majo, Resv Cat, Discount Rate, Net Oil Interest, Net Gas Interest, Working Interest, Gross Wells, Ultimate Oil, Ultimate Gas, Gross Oil, Gross NGL, Gross Gas, Net Oil, Net Gas, Net NGL, Revenue To Int., Oper. Expense, Total Invest., Revenue Oil, Revenue Gas, Operating Profit, Revenue NGL, Disc Net Income, SEQ, Well ID, INC ASN, Life Years, Own Qual, Production Tax, Ad Valorem Tax");
    int i = listHead2;

    //Loops until the end of the list, printing out info
    while (i != -1)
    {
        output.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}",
            QuoteString(leaseName2[i]), fieldName2[i], QuoteString2(reservoir2[i]), operator2[i], county2[i], state2[i], majo2[i], resvCatgory2[i], disRate2[i], netOil2Int2[i], netGas2Int2[i], workingInt2[i], grossWells2[i]
            , ultOil2[i], ultGas2[i], grossOil2[i], grossNGL2[i], grossGas2[i], netOil2[i], netGas2[i], netNGL2[i], revToInt2[i], operExpense2[i], totInvest2[i], revOil2[i], revGas2[i], operatingProfit2[i],
        revNGL2[i], discNetIncome2[i], seqNum2[i], wellID2[i], incASN2[i], lifeYears2[i], ownQual2[i], prodTax2[i], AdValorem2Tax2[i]);
        i = pointers2[i];
    }
}
4

2 回答 2

1

说第一个值line被第二个覆盖是不正确的。这是因为 C# 采用逻辑运算符的捷径。因此,如果您拥有(A || B)and Ais true,则B不会调用/检查。

while ((line = input2.ReadLine()) != null ||
       (line = input.ReadLine()) != null) //this is only called when the first value for line is null

也许您应该尝试将其拆分为两个while循环(或编写一个读取和输出给定输入的函数)。它将使其更具可读性,并且将避免input2在完全阅读后重复尝试阅读。

于 2013-06-03T14:21:57.357 回答
0

您对两者使用相同的字符串StreamReader。声明第二个字符串“ line2”以从第二个StreamReaderinput2”读取。

注意:你应该像这样声明你的变量:

String line = string.Empty;

您这样做的方式不会初始化变量。简单的好习惯。

于 2013-06-03T13:55:13.483 回答