| | |
Reading .dat file data into an array
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
Hi all... My first post in this forum... The problem is that of the String.Split method... I cant seem to get the String.Split method to work as I wanted... My objective is to try to read the data which are three columns of numbers and try to array them so that I can use them as points for contour plotting. The following is the data displayed in the file I tried to read from.
20311 20316 20321
20312 20317 20322
20313 20318 20323
20314 20319 20324
20315 20320 20325
My method is as follow:
while(objInput.Peek()>-1)
{
string strLine = objInput.ReadLine();
string [] split = strLine.Split(new Char [] {' '});
}
After which I can then initialise the split[0], split[1], split[2] etc to whatever arraylist i am going to use. However, these values are displayed as null value (blank) when I tried to console.writeline them.
The strange thing is when I edited the .dat file which I used to read the strings, and placed a comma instead of just spaces in between the words as illustrated below, the split method worked out fine when I used the following code in place of the above code.
20311 ,20316 ,20321
20312 ,20317 ,20322
20313 ,20318 ,20323
20314 ,20319 ,20324
20315 ,20320 ,20325
while(objInput.Peek()>-1)
{
string strLine = objInput.ReadLine();
string [] split = strLine.Split(new Char [] {','});
}
Can anyone help me with this problem? Did I use the String.Split method wrongly? Or what did I not do?
20311 20316 20321
20312 20317 20322
20313 20318 20323
20314 20319 20324
20315 20320 20325
My method is as follow:
while(objInput.Peek()>-1)
{
string strLine = objInput.ReadLine();
string [] split = strLine.Split(new Char [] {' '});
}
After which I can then initialise the split[0], split[1], split[2] etc to whatever arraylist i am going to use. However, these values are displayed as null value (blank) when I tried to console.writeline them.
The strange thing is when I edited the .dat file which I used to read the strings, and placed a comma instead of just spaces in between the words as illustrated below, the split method worked out fine when I used the following code in place of the above code.
20311 ,20316 ,20321
20312 ,20317 ,20322
20313 ,20318 ,20323
20314 ,20319 ,20324
20315 ,20320 ,20325
while(objInput.Peek()>-1)
{
string strLine = objInput.ReadLine();
string [] split = strLine.Split(new Char [] {','});
}
Can anyone help me with this problem? Did I use the String.Split method wrongly? Or what did I not do?
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
hrm... I understand where the error comes from already. It comes from the spaces before the numbers... which I never put in my question. The actual display is as shown below:
(spaces) 20311 20316 20321
(spaces) 20312 20317 20322
(spaces) 20313 20318 20323
(spaces) 20314 20319 20324
(spaces) 20315 20320 20325
and not the following:
20311 20316 20321
20312 20317 20322
20313 20318 20323
20314 20319 20324
20315 20320 20325
So, how do I solve this kind of problem?
(spaces) 20311 20316 20321
(spaces) 20312 20317 20322
(spaces) 20313 20318 20323
(spaces) 20314 20319 20324
(spaces) 20315 20320 20325
and not the following:
20311 20316 20321
20312 20317 20322
20313 20318 20323
20314 20319 20324
20315 20320 20325
So, how do I solve this kind of problem?
C# Syntax (Toggle Plain Text)
StreamReader objInput = new StreamReader("C:\\values.dat", System.Text.Encoding.Default); string contents = objInput.ReadToEnd().Trim(); string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None); foreach (string s in split) { Console.WriteLine(s); }
C# Syntax (Toggle Plain Text)
string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
C# Syntax (Toggle Plain Text)
StreamReader objInput = new StreamReader("C:\\values.dat", System.Text.Encoding.Default); string contents = objInput.ReadToEnd().Trim(); string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None); foreach (string s in split) { Console.WriteLine(s); }
I don't know if you are still around...
What do I need to modify in the "System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);" so the string will also expect whitespaces?
For example, in the .dat file you have:
Hello Word Test1 Test2 Test3
If you noticed, I have whitespaces between each words, I want to keep that.
so
string[0] = Hello
string[1] = " "
string[2] = Word
string[3] = " "
string[4] = Test1
string[5] = " "
..
..... and so on. Can this be done using the same Regex?
![]() |
Similar Threads
- Reading .dat file (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: how to close a DR and then open it again for use
- Next Thread: reading in .dat file data into an array - keeping whitespaces
| Thread Tools | Search this Thread |
.net access algorithm alignment app application array bitmap box c# c#gridviewcolumn check checkbox client color combo combobox communication concurrency control conversion csharp custom data database datagrid datagridview dataset datatable datetime degrees draganddrop drawing enabled encryption enum excel file focus form format forms function gdi+ getoutlookcontactusinfcsvfile globalization hospitalmanagementsystem image input install java list localization mandelbroth math messagebox microsoftc#visualexpress mono mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox save server sleep socket sql sql-server statistics string stringformatting sun table text textbox thread time timer update usercontrol validate validation visualstudio webbrowser winforms wpf xml





