Hello Michael,
Hello Michael,
Michael wrote:
Why bother answering if you are not prepared to help?
Is it just to prove how smart you are? I asked for a snippet
or a link.
If you want to decide on the form of help you get, then hire a
consultant. I am sure you can get somebody to do it for somewhere
between 50 and 100 dollars per hour.
If you want free help, then you will have to take it as it comes.
Arne
You guys are all up yourselves! The only reason I am doing
this is to help someone else make set covering algorithms work
faster! To see if C# can do it better than VB6.0. I have spent
many hours on the task. Why? Because as well as helping it
will improve my skills.
Good bye and good riddance!
Micheal,
I'm very sorry you feel this way. And I think it is great that you're
trying to help someone. And should you have formulated your question
differently, showed what you had aready created, or asked for the
most efficient way of doing it, and provided us with the original VB
code, then we would all have dug deep for you to find what you were
looking for, but even then, we'd probably provide you with the
building blocks, good advice and a collective review on your work so
far.
It's all in the way you've worded it...
Its OK Jesse, I've solved it. With no help from the self
appointed cyber police patrolling this group. I'm fixing
the code to this in case some other poor refugee from
VB6 searches the archives from this group. They would
not be expecting so complex a task, especially with the
hand grenade of double spaces between some of the .txt
data.
// Snippet for reading integers from .txt into a 2 d array
int[,] theLines = new int[163, 6]; //holds 163*6 elements
string path = @"C:\Documents and
Settings\Michael\Desktop\Wheel.txt";
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate,
FileAccess.Read));
int idx = 0;//
while (idx<163)
//THERE ARE DOUBLE SPACES BETWEEN SOME NUMBERS IN .txt
{
string row = textIn.ReadLine();
row = row.Trim();
row = row.Replace(" "," ");//get rid of double spaces
string[] columns = row.Split(' ');//redeclare in case
size changes
for (int j = 0; j < 6; j++)
theLines[idx,j ] = Convert.ToInt32(columns[j]);
idx++;
}
//Check all is well
for (int i = 0; i < 163; i++)
for (int j = 0; j < 6; j++)
{
Console.WriteLine(theLines[i, j]);
}
textIn.Close();
You can simplify your double space prblem by using the
String::Split Method (String[], StringSplitOptions)
overload. See
http://msdn.microsoft.com/en-us/library/tabh47cf.aspx. Like
so:
row.Split(new string[]{" ", " "}, StringSplitOptions.None);
Or you could use the StringSplitOptions.RemoveEmptyEntries like this:
row.Split(' ', StringSplitOptions.RemoveEmptyEntries);
This last option should negate the need to use row.Trim as well.
Another thing to note, is that it would be best to use a using(Disposable
object){} to ensure proper cleanup of your stream objects
I also added some error checking here and there...
public int[,] ParseFile(string path)
{
int[,] result= new int[163, 6]; //holds 163*6 elements
if (!File.Exists(path))
{
throw new FileNotFoundException();
}
using (StreamReader textIn = new StreamReader(new FileStream(path,
FileMode.Open, FileAccess.Read)))
{
while (int idx = 0; idx < 163; idx++)
{
if (textIn.Peak() == -1) // check for end-of-file
{
throw new Exception(string.Format("Unexpected end of file at line
{0}", idx + 1));
}
string row = textIn.ReadLine();
string[] columns = row.Split(' ',
StringSplitOptions.RemoveEmptyEntries); // StringSplitOptions removed
double spaces and trims values.
if (colums.Length == 6) // check expected input
for (int j = 0; j < 6; j++)
{
result[idx,j ] = Convert.ToInt32(columns[j]);
}
}
else
{
throw new Exception(string.Format("Expected 6 values on row {0},
found {1}", idx + 1, columns.Length));
}
}
}
return result;
}
I hope this partially restores your faith in these forums. It should at
least aid anyone who reads this thread to use this improved solution.