Results 1 to 2 of 2

Thread: WriteAllLines, ReadAllLines; StreamReader, StreamWriter?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Question WriteAllLines, ReadAllLines; StreamReader, StreamWriter?

    Using either the WriteAllLines/ReadAllLines or StreamReader/StreamWriter, how
    could you retrieve specific/ in-between information from the file "C:\Users.txt".

    The file "C:\Users.txt" looks like this >

    Code:
    #Users
    John
    Ashley
    
    #Stats
    John
    HP: 100
    Ashley
    HP: 100
    I am attempting to accomplish 2 simple tasks with the above information:

    1) Retrieve the list of users to a MessageBox.Show(ListofUsers)

    2) Retrieve Johns HP to a MessageBox.Show(JohnsHP)

    Can someone please show me how to do this by code example using the above
    information?

    Thanks, VBBBQ

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: WriteAllLines, ReadAllLines; StreamReader, StreamWriter?

    This is obviously an exercise/homework so it's really for you to write the code. We can help with a bit of instruction though.

    If you're going to use File.ReadAllLines then it will return a String array where each element is a line from the file. You then have random access to those lines, so you can access any one of them at any time by index. If you use a StreamReader then you will call ReadLine repeatedly, maybe in a loop, and you have only sequential access to the lines of the file. In this case, you only need to read forwards so either will do the job.

    Presumably you are allowed to assume that the file format is valid so there is no need to allow for invalid data. That means that you can be confident that the first line contains "#Users" and you can then read subsequent lines until you get an empty String and that is your list of users. You can then be confident that the next line contains "#Stats" and then you will have two lines for each item in the list of users that you read previously, with the first being the name and the second being the HP score.

    So, have a go at it and, if you still have issues, post back here and show us what you've tried, explain your reasoning and tell where reality differs from expectation. We can then help you to fix the issues yourself. The best way to learn is to do so you should do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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