|
-
Oct 6th, 2003, 01:48 PM
#1
Thread Starter
Lively Member
Reading words from text files
I want to read a text file consisting of single words on a line each e.g.
apple
pear
orange
banana
... and put the words into a string array.
So far I've come up with:
string[] sK;
StreamReader K = new StreamReader(sFile);
sK = K.ReadToEnd().Split('\n');
But, in the Autos window, I can see that each word has come out like this:
appleX
pearX
orangeX
bananaX
(where X is a box shaped character).
What is it and how could I remove it?
-
Oct 6th, 2003, 02:00 PM
#2
the box that appears is it trying to render the \n. try using ReadLine()
-
Oct 6th, 2003, 07:16 PM
#3
Addicted Member
or try splitting by \r\n instead of just \n
-
Oct 6th, 2003, 08:16 PM
#4
try this...
VB Code:
private void button1_Click(object sender, System.EventArgs e)
{
System.IO.StreamReader s=new System.IO.StreamReader(new System.IO.FileStream(@"C:\test.txt",System.IO.FileMode.OpenOrCreate));
while(s.Peek() !=-1)
{
MessageBox.Show(s.ReadLine().ToString());
}
}
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 16th, 2003, 02:53 PM
#5
Lively Member
The box is the \r character, \r is a carriage return, which tells the computer to print what's passed it to the left of the box, this does not mean however that the text will be on a new line, thus the \n character is also required as it means move to a new line, thus: \r\n = return to begginning of line, move to next line.
If the computer acted like a typewriter what would happen with just a \r and no \n is that you would have the thing that types move all the way to the left and start typing over the same line it just did! That would be very hard to read!
Anyways just split it at \r\n like Tewl said.
btw for people who don't know \r\n are not characters, they just symbolize the characters we want, the "box" (X) that you are seeing, in VB you would use vbCrLf(carriage return, line feed), also if the textbox was set to multiline, you might not see the box, just the text being split over new lines
Mitchel
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|