Results 1 to 5 of 5

Thread: Reading words from text files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91

    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?

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    the box that appears is it trying to render the \n. try using ReadLine()

  3. #3
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188
    or try splitting by \r\n instead of just \n

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    try this...
    VB Code:
    1. private void button1_Click(object sender, System.EventArgs e)
    2. {
    3.     System.IO.StreamReader s=new System.IO.StreamReader(new System.IO.FileStream(@"C:\test.txt",System.IO.FileMode.OpenOrCreate));
    4.     while(s.Peek() !=-1)
    5.     {
    6.         MessageBox.Show(s.ReadLine().ToString());
    7.     }
    8. }
    ~
    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]

  5. #5
    Lively Member
    Join Date
    Sep 2002
    Posts
    100
    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
  •  



Click Here to Expand Forum to Full Width