Results 1 to 17 of 17

Thread: Random Access Text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Random Access Text file

    I'm doing a Program for a Friend of mine, He Want to have a Console Program Running on his Computer.
    OK the problem i am having is With Stream Read, I have 3 test users inside Test File,

    if User 1 logs in his information is store in logged file with time and data.

    but if User 3 logs on user 1 2 and 3 are all saved on the logged file

    i dont have read to end of file in my Code.

    i dont have my Program with me, so i going off the top of my head i have something like this

    what i want to be able to find out how to do is Right from any part of the file and out put it, I take it i need to use Random Access but how to i code Random Access for this type of file.
    All i can find is how to Randomly place Words in a text file with Length and size.

    Code:
    Dim sgruser As New Stream.IO.Reader("User")
    Dim "" As String
    ""
    ""
    ""
    sgruser.close()
    
    Dim input As StreamWriter("Logged")
    input.Writeline()
    input.Writeline()
    input.close()

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

    Re: Random Access Text file

    You can't really randomly access text files unless you know exactly how long each file/line is. Basically you have two options:

    1. Read the file field by field and or line by line, discarding any data you don't want as you go until you reach the data you do want.

    2. Read all the data into a data structure and then randomly access that, e.g. a collection of objects.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    ok, what is the best Choice to Save information of a User as they log on?

    Their be around 10 users in this User file

    and if the First user or the last user logs on how do i get the Readline know where to look and output it only that user not any other one

    Thats were im getting stuck, Search Every where on the net but no joy, and really this place is my last chance.

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

    Re: Random Access Text file

    One reason you're having trouble is the fact that text files are not the ideal way to store and search this sort of information. Databases are much more natural for this sort of thing.

    If you want to stick with a text file then the whole point is that you DON'T know where to look. If you want to determine whether there is matchiong information in the file you simply have to read the file line by line or whatever and then you'll know the data when you find it. If you get to the end without finding it then you know that it's not there.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    I'm using Text Files,

    As i dont know how to use Access for VB, Nor do i have Microsoft Office install on my pc.
    the Whole Program is done and finished only for this small part which at this stage im hitting my head off the wall. as i dont have a clue on how to solve this Error.

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

    Re: Random Access Text file

    As I have already said, read the file line by line until you find the specific line you're after, or else read all the data into memory and then access that randomly. If you go for the first option you could use a StreamReader or a TextFieldParser. For the second option you'd probably call File.ReadAllLines and then process the resulting String array however is appropriate for the data format.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    I have stream reader working and outputting but my problem is if more then 1 user is in the file it's the 2 users

    If I have 4 users jack,bob,jim,john if Bob logs on with his username and password the computer should say Bob is logged in I must read bobs details and output them to a different text file but jack,bob,jim and john are all being output to the text file how can I make the computer red them ll till it finds bob and the exit read and move to write

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

    Re: Random Access Text file

    Obviously there's something wrong with your code, but you haven't shown us your code so we can't say what.
    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

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Random Access Text file

    Technically, if you take your text file as a binary one some resemblance of random access can be implemented. This of course would not be a good solution but theoretically it's possible.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    ok here is the read line and write line code i have in

    Code:
      Dim Input As New IO.StreamReader("Save.txt")
    Input = File.OpenText("Save.txt")
                   Company =  Input.ReadLine()
                   Telephone = input.Readline()
                   Name = input.Readline()
                   Address = input.readline()
                   Username = input.readline()
                   pass = input.readline()
    
               
                    Input.Close()
    
                    Dim Output As  IO.StreamWriter("file.txt")
                    Output = File.AppendText("file.txt")
    
                    Output.WriteLine("Company.text")
                     Output.WriteLine("Telephone.text")
    Output.WriteLine("Name.text")
                    Output.WriteLine("Address.text")
    Output.WriteLine("User.text")
    Output.WriteLine("pass.text")
    
                    Output.Close()

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

    Re: Random Access Text file

    When you read the file you are simply reading the first set of data and closing. You aren't ever checking that data to make sure it's the set you want. Once you've read a set of data you need to check it to make it's what you want. If it's not you discard it and read the next set. This goes on until you find the data you're after or you reach the end of the file, so you need to check for that too.
    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

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    That's what I can't figure out how to do

    What code would I need to enter to get it to work

  13. #13
    Member
    Join Date
    Apr 2010
    Posts
    52

    Re: Random Access Text file

    for line in input do this if y = x

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    It if y= x then
    Name=input.readline()
    else

    readline is false

    next

    Writeline

    is it like that?

  15. #15
    Member
    Join Date
    Apr 2010
    Posts
    52

    Re: Random Access Text file

    That's the basic idea of it, yeah.

    I've only been here a few days, so I'm still getting a feel for the place, but it seems like the general idea is to give people a direction in which to focus their research, which is understandable, as if we just typed the code in for you then you'd copy / paste and not really know what was happening.

    However, I'm going to go ahead and agree with those above - a database is the best way to go with this. If not a database, then at least use an .xml file instead of .txt as it'll make retrieving records for individual queries a lot more straightforward in the long run.

    http://www.codeproject.com/KB/cpp/parsefilecode.aspx

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

    Re: Random Access Text file

    The way to solve problems like this is to forget that you're writing a computer program and ask yourself what you would do if this was a manual process. Work out what steps you would have to perform if you were doing it physically, e.g. with pen and paper. Those steps form an algorithm. Once you have a working algorithm, then you can start writing code to implement it. Because you have a series of steps you can write code to implement one step at a time, so the whole thing becomes much easier.

    So, what would be the steps here?

    1. Open file.
    2. Check if you are at the end of the file.
    2a. If you are at the end of the file then exit.
    3. Read a record.
    4. Check if that record is the one you are interested in.
    4a. If you have the record you want then exit.
    5. If you don't have the record you want then go back to step 2 and continue.

    As you can see, the steps are very simple and logical. If you can't come up with a set of simple and logical steps to perform to complete the task then how can you possibly complete the task? How can you do something if you don't know what you have to do? The steps are always the first step.
    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

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    17

    Re: Random Access Text file

    I have
    Code:
    Dim ty as string
    ty=input.readline
    
    if username.text = usernam. Then
    
    readline
    
    end If
    loop
    
    writeline()
    now all the name nd telephone in writeline are stating ther being used
    before they are declared
    this is the last part of the program I need to do and it's wrecking my head just feel like kicking this laptop out my top window..

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