I figured this would be an easy solution, however I've tried to approach this from many different angles with no success.
Here's a quick breakdown on what I'm trying to accomplish.
1. Read the text file with values separated by commas aka "CSV"
2. Use a while loop to go through the data and split the lines into 3 columns
a) Loop the read data into a list
b) Loop the read data onto the textbox
I can print off the first row, however I'm trying to figure out how/why I'm unable to loop the remaining lines within the roster.txt file. Any help would be greatly appreciated. Thanks.
private void button4_Click(object sender, EventArgs e)
{
StreamReader roster = new StreamReader(@"c:\files\roster.txt");
string roster_data = roster.ReadLine();
string[] splitter = roster_data.Split(',');
//string roster_data;
int x = 0;
List
//var playerlist = new List
while (x < splitter.Length)
{
string player = Convert.ToString(splitter[x]);
double salary = Convert.ToDouble(splitter[x + 1]);
string position = Convert.ToString(splitter[x + 2]);
string combined = (string.Format("{0} {1:c2} {2}", player, salary, position));
x = x + 3;
playerlist.Add(combined);
textBox1.AppendText(combined);
}
roster.Close();
//while ((roster_data = roster.ReadLine()) != null)
//{
// playerlist.Add(roster_data.Split(','));
//}
//foreach (string[] s in playerlist)
//{
// string first = playerlist[0][0];
// string second = playerlist[0][1];
// string combined = (first + second);
// textBox1.AppendText(combined);
//}
}
Vishal GilbilePosted Apr 19, 2013, 4:32 AM
The following line of code may help you
const string filePath = @"D:\testData.txt";
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
string[] data;
List
while (str != null)
{
data = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string combinedEntry = string.Format("{0}:{1}:{2}", data[0], data[1], data[2]);
lstData.Add(combinedEntry);
str = sr.ReadLine();
}
fs.Close();
sr.Close();
foreach (string st in lstData)
{
Console.WriteLine(st);
}
With Regards,
Vishal Gilbile
Curtis SchreinerPosted Apr 19, 2013, 1:42 PM
This what I was looking for, and I realized I needed to include the Readline() both outside & inside the while loop.
With a few modifications from your script got the results I was looking for.
StreamReader roster = new StreamReader(@"c:\files\roster.txt");
string str = roster.ReadLine();
string[] roster_data;
List
while (str != null)
{
//roster_data = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
roster_data = str.Split(',');
string player = Convert.ToString(roster_data[0]);
double salary = Convert.ToDouble(roster_data[1]);
string position = Convert.ToString(roster_data[2]);
string combined = (string.Format("{0}\t{1:c2}\t{2}\n", player, salary, position));
roster_list.Add(combined);
str = roster.ReadLine();
}
roster.Close();
foreach (string st in roster_list)
{
textBox1.AppendText(st);
}
Jignesh TrivediPosted Apr 19, 2013, 8:37 AM
you may also try with following code
var stringdata = File.ReadAllLines(@"E:\JTtest.txt");
for (var recordCount = 1; recordCount < stringdata.Length; recordCount++)
{
var data = stringdata[recordCount].Split(new char[] { ',' });
// do your logic
Console.WriteLine(stringdata[recordCount]);
}
hope this will help you.