Reputation: 111
I am trying to write data that has been read from a text file and write it to another text file into a comma delimited format. I need to know what the code is to come to that conclusion. This where I need the help.
Example:
Original Data looks like this:
Agnico-Eagle Mines
COM
008474108
28996843
716800
716800
N/A
N/A
N/A
716800
N/A
Agrium Inc.
COM
008916108
145739616
1646617
1646617
N/A
N/A
N/A
1646617
N/A
AuRico Gold Inc
COM
05155C105
504505
62875
62875
N/A
N/A
N/A
62875
N/A
This is how I want the data to look like in the RichTextBox:
Agnico-Eagle Mines,COM,008474108,28996843,716800,716800,N/A,N/A,,N/A,716800,N/A
Agrium Inc.,COM,008916108,145739616,1646617,1646617,N/A,N/A,,N/A,1646617,N/A
AuRico Gold Inc,COM,05155C105,504505,62875,62875,N/A,N/A,,N/A,62875,N/A
Just so you know from the original text data, I want to read the first line, then add a comma and then read the 2nd line append it to the first line then add a comma, and this goes one for the 1st 12 lines. The end of the 12th line has no comma. Then the process starts over again.
Any info is appreciated.
Thanks.
Below is the code that I have written thus far.
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
while (!Reader.EndOfStream)
{
TextBox1.AppendText(Reader.ReadLine());
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
Writer.WriteLine(TextBox1.Text);
}
}
Upvotes: 1
Views: 6034
Reputation: 39
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
var i = 0;
while (!Reader.EndOfStream)
{
if (i % 12 == 11)
{
//every 12 lines you read append new line instead of ,
TextBox1.AppendText(string.Format("{0}\n", Reader.ReadLine()));
}
else
{
TextBox1.AppendText(string.Format("{0},", Reader.ReadLine()));
}
i++;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
using (StringReader Reader = new StringReader(TextBox1.Text))
{
while (true)
{
var line = Reader.Readline()
if(line !=null)
{
Writer.WriteLine(Reader.Readline());
}
else
{
break;
}
}
}
}
}
Upvotes: 0
Reputation: 35353
Assuming 12 is the magic number for your input text,
var query = File.ReadLines("a.txt")
.Select((line,no) => new{line,no})
.GroupBy(x => x.no/12)
.Select(g => String.Join(",",g.Select(x => x.line)));
File.WriteAllLines("b.txt",query);
This works for your sample input and expected output....
Upvotes: 5
Reputation: 44931
This is the way that I would approach reading the data:
var sbText = new System.Text.StringBuilder(10000);
// Keeps track of your current position within a record
int wCurrLine = 0;
// Number of rows in the file that constitute a record
const int LINES_PER_ROW = 12;
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
while (!Reader.EndOfStream)
{
// If we are not on the first row in the record, add a comma
if (wCurrLine != 0)
{
sbText.Append(",");
}
// Add the text
sbText.Append(Reader.ReadLine());
// Increment our current record row counter
wCurrLine++;
// If we have read all of the rows for this record
if (wCurrLine == LINES_PER_ROW)
{
// Add a line to our buffer
sbText.AppendLine();
// And reset our record row count
wCurrLine = 0;
}
}
// When all of the data has been loaded, write it to the text box in one fell swoop
TextBox1.Text = sbText.ToString();
EDIT: I just realized I didn't answer the original question fully: there is no reason to use the textbox unless you want to see the results before writing them out. If you don't need to do this, you can replace the line:
TextBox1.Text = sbText.ToString();
with:
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.csv"))
{
Writer.Write(sbText);
}
(Note the change of extension in the file name).
Upvotes: 6
Reputation: 33738
create a List<String>
, read each line into it.. then String.Join the List using a ,
as a separator. Of course call the Clear
method on the list at the beginning of each line.
Upvotes: 0
Reputation: 70523
NB - I'm leaving this as an example of how to do this if there was a marker (blank line), I miss read the example data when I wrote it.
Something like this should work, I did not test so it might have typos:
Note: I look for a blank line (instead of counting) to know I'm at the end of a record. In this way if you add another field the code won't break.
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
StringBuilder aLine;
boolean first = true;
while (!Reader.EndOfStream)
{
// read source line
string inLine = Reader.ReadLine();
// if length is zero append to test box (we have a blank line between records)
if (inLine.Length == 0)
{
TextBox1.AppendText(aLine.ToString());
first = true;
}
// add a comma if we are not the first
if (!first)
{
aLine.Append(",");
}
aLine.Append(inLine);
// next time we won't be first
first = false;
}
TextBox1.AppendText(aLine.ToString());
}
Upvotes: 0
Reputation: 38608
If you just want to write from one file to another one with differecnt method, try something like this:
protected string TextProperty { get; set; }
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
// read all content file to the string property on your form.
TextProperty = reader.ReadToEnd();
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
// write all content of the property to the file.
writer.Write(TextProperty);
}
}
if you want to separate line by line or do a specifict reatment for each line, you could use StringBuilder
class.
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
StringBuilder builder = new StringBuilder();
while (reader.Peek() >= 0)
{
builder.AppendLine(reader.ReadLine());
}
}
}
and to get all the entire content of the StringBuilder
just call the ToString()
method and write it on the file:
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
writer.Write(builder.ToString());
}
}
Upvotes: 1