Reputation: 87
This may not be a very good question, but I am trying to output two files to one CSV file. I'm using a while
loop to read through the two files, and then I'm calling a method that prints it all out, but when it prints out, I'm not getting all of the information that is in both of the files. Here is my while
loop code:
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();
}
I am really not sure why it is not outputting all of the information in the files, any help would be much appreciated. And I'll put my printall();
method in here just in case that's where the problem is as well:
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];
}
}
Upvotes: 0
Views: 129
Reputation: 767
It is not correct to say that the first value for line
is being overridden by the second. This is because C# takes shortcuts with logical operators. So if you have (A || B)
and A
is true
, then B
is not called/checked.
while ((line = input2.ReadLine()) != null ||
(line = input.ReadLine()) != null) //this is only called when the first value for line is null
Perhaps you should try splitting this into two while
loops (or write a function that reads and outputs a given input). It will make it more readable, and it will avoid repeatedly trying to read input2
after it has been fully read.
Upvotes: 1
Reputation: 101
You are using the same string for both StreamReader
. Declare a second string "line2
" to read from the second StreamReader
"input2
".
Note : You should declare your variables like that:
String line = string.Empty;
The way you are doing it does not initialize the variable. Simply good practices.
Upvotes: 0