Hey there,
I can't seem to find any questions/solutions throughout the web that correlate to this particular scenario, so I figured I'd give a breakdown on what I'm trying to achieve using the StreamReader type.
Basically I have 2 files with unbalanced lines counts i.e. data1.txt contains contains 20 lines, whereas data2.txt contains 10, so I'm using StreamReader to first read data from both .txt files, and I was thinking I could use the while (((ts.transaction = t.ReadLine()) !=null)||((ms.master = t.ReadLine()) !=null)) to read the total lines from both files, and then I could continue with applying additional logic to merge my data into a 3rd file afterwards.
However, when I run the following below, I'm running into a "Object reference not set to an instance of an object" likely because of the unbalanced line count? It seems to work when I replace "||" in the while statement with "&&" however I'm unable to print the total number of lines from both files.
At the moment I'm just appending the text to richTextBox1 in order to test my data output for now. I was seeing if there's a better way I can go about using the OR clause, or whether I'm going about this while condition the wrong way?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace credit_card
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Read Transaction File
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
StreamReader t = new StreamReader(@"c:\data1.txt");
StreamReader m = new StreamReader(@"c:\data2.txt");
transaction_storage ts = new transaction_storage();
master_storage ms = new master_storage();
while (((ts.transaction = t.ReadLine()) !=null)||((ms.master = t.ReadLine()) !=null))
//while ((ts.transaction = t.ReadLine()) != null)
{
ms.m_index = Convert.ToInt32(ms.master.Substring(0, 2));
ts.t_index = Convert.ToInt32(ts.transaction.Substring(0, 2));
ts.t_name = ts.transaction.Substring(2, 10);
ts.t_item = ts.transaction.Substring(10, 17);
ts.t_amount = Convert.ToDouble(ts.transaction.Substring(ts.transaction.Length -7, 7));
string transaction_data = (ts.t_index.ToString() + " " + ts.t_name + " " + ts.t_item + " " + ts.t_amount + "\n");
string master_data = (ms.m_index.ToString());
richTextBox1.AppendText(transaction_data);
richTextBox1.AppendText(master_data);
}
t.Close();
m.Close();
}
class master_storage
{
public int m_index;
public string master;
}
class transaction_storage
{
public int t_index;
public string t_name;
public string t_item;
public double t_amount;
public string transaction;
}
}
}
Loading
Curtis SchreinerPosted Mar 25, 2013, 1:18 PM
This certainly worked as far as interweaving both data files, now seeing that I have to do further comparisons on the files afterwards, I'm curious if I've painted myself into a corner and need to start fresh with a different approach.
Basically one of my objectives is that I want to compare the ID's in data1.txt & ID in data2.txt matchup, then add the dollar amount from data1.txt into data2.txt when this criteria is met.
I originally anticipated this would be an easier solution, however it's becoming quite cumbersome when doing line comparisons across to 2 files with different line counts. I've applied an IF statement when the m_index & t_index ID's matchup, however it doesn't really do much except find one match, and then disregard. I've tried applying this logic into FOR loops where the data was dumped into array List/SortedList's, however I'm unable to achieve the initial objective when wrapping everything into the while (((ts.transaction = t.ReadLine()) !=null)||((ms.master = t.ReadLine()) !=null)) statement. Here's the latest code I have in the attachment.
VulpesPosted Mar 24, 2013, 6:08 AM