TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Curtis Schreiner
NA
10
38.6k
Streamreader with multiple files
Mar 23 2013 8:42 PM
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;
}
}
}
Reply
Answers (
2
)
C#.Net using MS Access Database Backup and Restore Requereme
Retrieve data Grideview when SelectedIndexChanged