944,098 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 30764
  • C# RSS
Jan 8th, 2006
0

Reading .dat file data into an array

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 8th, 2006
0

Re: Reading .dat file data into an array

Hi, i tried out your code and it seems to work even with the spaces, however one remark: i don't know if it was your intention to initialize the split variable inside the loop...so keep in mind that it will be overwritten all the time and just keeps the latest 3 values (from the last readline)
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Jan 9th, 2006
0

Re: Reading .dat file data into an array

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 9th, 2006
0

Re: Reading .dat file data into an array

C# Syntax (Toggle Plain Text)
  1. StreamReader objInput = new StreamReader("C:\\values.dat", System.Text.Encoding.Default);
  2. string contents = objInput.ReadToEnd().Trim();
  3. string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);
  4. foreach (string s in split)
  5. {
  6. Console.WriteLine(s);
  7. }
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Jan 11th, 2006
0

Re: Reading .dat file data into an array

Wow... Thanks alot, rOckbaer! It worked perfectly! I will now try to read them into an array. Any idea how it can be done? Its of two dimensional array, right?
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 11th, 2006
0

Re: Reading .dat file data into an array

C# Syntax (Toggle Plain Text)
  1. string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);
split is already a string array
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Jan 12th, 2006
0

Re: Reading .dat file data into an array

Thanks again! May I know what the \\s+ means? Cant seem to find it on the web.
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 12th, 2006
0

Re: Reading .dat file data into an array

\s+ is a regular expression, the \s means to consider whitespaces and the + means to consider at least 1 occurence or more of them
Reputation Points: 13
Solved Threads: 7
Light Poster
_r0ckbaer is offline Offline
45 posts
since Dec 2005
Jan 13th, 2006
0

Re: Reading .dat file data into an array

Ah.... IcIc... Thats exactly what I need! No wonder it worked perfectly!

Thanks alot!
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Oct 24th, 2007
0

Re: Reading .dat file data into an array

Click to Expand / Collapse  Quote originally posted by _r0ckbaer ...
C# Syntax (Toggle Plain Text)
  1. StreamReader objInput = new StreamReader("C:\\values.dat", System.Text.Encoding.Default);
  2. string contents = objInput.ReadToEnd().Trim();
  3. string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\\s+", RegexOptions.None);
  4. foreach (string s in split)
  5. {
  6. Console.WriteLine(s);
  7. }
Hi _r0ckbaer,

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tphuynh is offline Offline
3 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to close a DR and then open it again for use
Next Thread in C# Forum Timeline: reading in .dat file data into an array - keeping whitespaces





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC