Solved

How to get comma separated values of Log file and store in array

Posted on 2009-05-19
2
196 Views
Last Modified: 2013-12-17
i have huge log file with all element comma(,) separated. I need to read line by line in c#  .net and store each values in array.

Can any one suggest the best code in c# to do this
0
Comment
Question by:mohantyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
RyanAndres earned 125 total points
ID: 24423797

StreamReader reader = new StreamReader("file.txt");
 
string line;
while ((line = reader.ReadLine()) != null)
{
    string[] lineParts = line.Split(',');
    //  Do what you want with lineParts array.
}

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24424280
I don't see anything wrong with the above, but I can suggest an alternative. Also, I'm assuming your file is line-delimited, and within the lines, it is comma delimited. If the comma-delimited values belong to the line, you can use this, instead:

private System.String[][] OrganizeFile(System.String myFilePath)
{
	System.String[] temp = System.IO.File.ReadAllLines(myFilePath);
	System.String[][] output = new System.String[temp.Length][];
 
	for(System.Int32 i = 0; i < temp.Length; i++)
	{
		output[i] = temp[i].Split(',');
	}
 
	// The first "index" indicates the line, while the second indicates each value.
	return output;
}

Open in new window

0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question