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.
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.
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.
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.
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.
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
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.
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.
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()
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.
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
Re: Random Access Text file
for line in input do this if y = x
Re: Random Access Text file
It if y= x then
Name=input.readline()
else
readline is false
next
Writeline
is it like that?
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
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.
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..