Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Some questions for VB: reading files, adressing types

NiuxNiux Member Posts: 12
[b][red]This message was edited by Niux at 2005-6-30 8:18:1[/red][/b][hr]
hey! i just finished my first VB book, and wanted to do something my self, so i started a little game projekt..

anyways, i have some problems:

i got i txt file with some data, looks like this:
some computers name
192.168.xxx.xxx
some other computers name
192.168.xxx.xxx

so now my problem is, how to i read every 2nd line in a text file?
i read a file like this:

do while EOF(1) = false
line input #1, thelinevar
loop
close #1

and is it possible to adress the diffent lines i read to diffent places in a Type?

how to delete a line from a file? - if got some code that searches for a string in a file, but dont know how to delete that string when its found. (and it gotta move up the line after the one deleted, so i dont get something like this:
blahblahb blah
blah blah
weeee

something
some more

with empty lines in my file)

Another question:
i have a txtfield in witch i type commands - in my program - and atm i type in the command and click on a button, and then the program gets the vaule of the txtfield. i want it to get the value when i hit Enter and keep focus in the txtfield, but how?

thx for the help,
Niux


Comments

  • PrzemekG_PrzemekG_ Member Posts: 595
    :
    : i got i txt file with some data, looks like this:
    : some computers name
    : 192.168.xxx.xxx
    : some other computers name
    : 192.168.xxx.xxx
    :
    : so now my problem is, how to i read every 2nd line in a text file?
    : i read a file like this:
    :
    [code]
    do while not eof(1)
    line input #1, pc_name
    if not eof(1) then
    line input #1, pc_address
    ' you probably want to do some thing with those values before overwriting them
    end if
    loop
    close #1
    [/code]
    The "If not eof(1) then ..." if to ensure that you won't get any error if a PC name is not followed by an IP (you should always remember to search for possible bugs in your program and remove them)


    : and is it possible to adress the diffent lines i read to diffent places in a Type?
    YES!
    [code]
    Type RemotePC
    Name as String
    IP as String
    End type

    Dim somePC as RemotePC

    ' replace the input in the above examples with those
    line input #1, somePC.Name
    line input #1, somePC.IP
    [/code]


    : how to delete a line from a file? - if got some code that searches for a string in a file, but dont know how to delete that string when its found.

    In general, that is the diferance between databases and simple files :)
    While database drives supports Add (Append), Update, Remove the file IO routines allows you to only to Add and Update - in most languages you have also truncate, but I don't remeber if VB have it too :)
    Anyway, you can do it by writing some more complex code (but still it should be quite simple):

    The easyest way (for me since I don't know the truncate command) is to read the entire file line by line and store each of them in an array or collection skipping only the one you want to delete. Then you close the file, open it again for "Output" mode and write each of the lines you stored in your array or collection back to the file

    : with empty lines in my file)
    :
    : Another question:
    : i have a txtfield in witch i type commands - in my program - and atm i type in the command and click on a button, and then the program gets the vaule of the txtfield. i want it to get the value when i hit Enter and keep focus in the txtfield, but how?

    This is a task for a hardcore VB programmer since you whould need to subclass the control :) This is a VB limitation (the price you pay for simplicity and fast developement). You can catch the Enter hit using KeyDown event (key code is vbKeyReturn) but VB will always play the "BEEP" sound. I think I solved this problem long time ago by changing the value of KeyCode to some printable character, but it was a lot of work.


    I hope this helps you.
Sign In or Register to comment.