Reading from file into an array

M

Michael

Say I have integers stored in a text file at "C:\myints.txt"
Like so.....

1 2 5 11 12 25
1 2 14 22 26 27
1 3 4 12 20 27
1 3 5 18 19 22
1 3 6 13 16 19

I want to get them from the file into a 2 dimension
integer array. Anybody have a snippet or link that
can get that done?
TIA Mick
 
P

Pavel Minaev

Say I have integers stored in a text file at "C:\myints.txt"
Like so.....

1  2  5 11 12 25
 1  2 14 22 26 27
 1  3  4 12 20 27
 1  3  5 18 19 22
 1  3  6 13 16 19

I want to get them from the file into a 2 dimension
integer array. Anybody have a snippet or link that
can get that done?

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.
 
P

Pavel Minaev

Say I have integers stored in a text file at "C:\myints.txt"
Like so.....

1  2  5 11 12 25
 1  2 14 22 26 27
 1  3  4 12 20 27
 1  3  5 18 19 22
 1  3  6 13 16 19

I want to get them from the file into a 2 dimension
integer array. Anybody have a snippet or link that
can get that done?

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.
 
M

Michael

Say I have integers stored in a text file at "C:\myints.txt"
Like so.....

1 2 5 11 12 25
1 2 14 22 26 27
1 3 4 12 20 27
1 3 5 18 19 22
1 3 6 13 16 19

I want to get them from the file into a 2 dimension
integer array. Anybody have a snippet or link that
can get that done?

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.

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.
 
M

Michael

Say I have integers stored in a text file at "C:\myints.txt"
Like so.....

1 2 5 11 12 25
1 2 14 22 26 27
1 3 4 12 20 27
1 3 5 18 19 22
1 3 6 13 16 19

I want to get them from the file into a 2 dimension
integer array. Anybody have a snippet or link that
can get that done?

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.

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.
 
P

Pavel Minaev

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.

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.

This is not a "solve my homework" newsgroup. This is a newsgroup for
questions related to C# programming language. "I don't know how to do
<generic high-level task>" isn't such a question, even if you add "...
in C#" to it. The nature of the problem is such that it is solved in
the same generic way in any imperative programming language, so
there's nothing really C#-specific here (well, there is, and I've
listed it in my previous post). If you are unable to even contemplate
how to approach this, then you need to learn the fundamentals of
programming first, and there are places dedicated to that; this isn't
one of them.

If you expect to be helped, you should at least bother enough to try
to solve this on your own, and if you have any _specific_ questions
(such as "how do I read a line from a text file?", or "how do I
extract all numbers from the line?", or "how do I initialize a 2D
array?"), then you're welcome to ask them, and I'll do my best to
answer them. But, as it is, if you yourself are unwilling to dedicate
any amount of your time to solve this (and I don't know how else to
interpret your original post, since there's no indication in it
whatsoever that you've even tried to solve this on your own), then why
should anyone else bother to do it for you, for free?
 
P

Pavel Minaev

What exactly is the problem? Between StreamReader.ReadLine,
String.Split, and Int32.Parse, you should have all the pieces you
need.

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.

This is not a "solve my homework" newsgroup. This is a newsgroup for
questions related to C# programming language. "I don't know how to do
<generic high-level task>" isn't such a question, even if you add "...
in C#" to it. The nature of the problem is such that it is solved in
the same generic way in any imperative programming language, so
there's nothing really C#-specific here (well, there is, and I've
listed it in my previous post). If you are unable to even contemplate
how to approach this, then you need to learn the fundamentals of
programming first, and there are places dedicated to that; this isn't
one of them.

If you expect to be helped, you should at least bother enough to try
to solve this on your own, and if you have any _specific_ questions
(such as "how do I read a line from a text file?", or "how do I
extract all numbers from the line?", or "how do I initialize a 2D
array?"), then you're welcome to ask them, and I'll do my best to
answer them. But, as it is, if you yourself are unwilling to dedicate
any amount of your time to solve this (and I don't know how else to
interpret your original post, since there's no indication in it
whatsoever that you've even tried to solve this on your own), then why
should anyone else bother to do it for you, for free?
 
A

Arne Vajhøj

Michael said:
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
 
A

Arne Vajhøj

Michael said:
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
 
M

Michael

Arne Vajhøj said:
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!
 
M

Michael

Arne Vajhøj said:
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!
 
C

Cor Ligthert[MVP]

Michael,

Pavel gave you the all the needed ingredients,
Pete has measured it for you in detail.
As that is even to difficult, Arne gave you an alternative.

What do you expect more?

It would be easier for Pete to give you the fish then let you catch it
yourself.
However, he took some more work, because what you have to do, can be done in
I guess in about 10 lines of code.

But in these newsgroups we want to help to get things better done, which
does not mean write the code.

Cor
 
C

Cor Ligthert[MVP]

Michael,

Pavel gave you the all the needed ingredients,
Pete has measured it for you in detail.
As that is even to difficult, Arne gave you an alternative.

What do you expect more?

It would be easier for Pete to give you the fish then let you catch it
yourself.
However, he took some more work, because what you have to do, can be done in
I guess in about 10 lines of code.

But in these newsgroups we want to help to get things better done, which
does not mean write the code.

Cor
 
J

Jesse Houwing

Hello Michael,
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...
 
J

Jesse Houwing

Hello Michael,
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...
 
M

Michael

Jesse Houwing said:
Hello Michael,


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();
 
M

Michael

Jesse Houwing said:
Hello Michael,


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();
 
J

Jesse Houwing

Hello Michael,
Hello Michael,

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.
 
J

Jesse Houwing

Hello Michael,
Hello Michael,

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.
 
M

Michael

Jesse Houwing said:
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.
Thanks Jesse, I will implement your recommendations as I am sure
they will make my code a lot more bullet proof.
Cheers Mick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top