Thread

Open/Close Files (Bots)

Open/Close Files // Bots

1  |  

lanezeri

Oct 17, 2001, 3:57pm
I asked this before and got a few responses, but could you refresh my memory
on how to open files and close them once they are edited?

--

Lanezeri
Lead Bot Programmer at Stuff-X
http://aw.stuff-x.com

foxmccloud

Oct 17, 2001, 4:06pm
You didn't tell what language you use...

Fox Mc Cloud

"lanezeri" <Lanezeri at stuff-x.com> a écrit dans le message news: 3bcdc66e at server1.Activeworlds.com...
> I asked this before and got a few responses, but could you refresh my memory
> on how to open files and close them once they are edited?

lanezeri

Oct 17, 2001, 5:06pm
VB

--

Lanezeri
Lead Bot Programmer at Stuff-X
http://aw.stuff-x.com


[View Quote]

trekkerx

Oct 18, 2001, 12:54am
I only rember the Print File commands off the top of my head. Im to lazy to
go and read it from one of my programs. I use C++ now so I dont use VB as
much.

'Print to File
Public Sub PFile(File as string, message as string)

Open File as output as #1
print #1, message
Close #1

Exit Sub

I hope it works Lanezeri.

[View Quote] > VB
>
> --
>
> Lanezeri
> Lead Bot Programmer at Stuff-X
> http://aw.stuff-x.com
>
[View Quote] --
TrekkerX - CEO
Commatron
http://www.commatron.com

grimble

Oct 18, 2001, 9:24am
Open logFile for Append as #1

.... will open the file and continue writing at the end of the existing data.
I remember saying a load of other stuff last time you asked, so you might
want to dig the old post out anyway. You have a number of options for how to
use it programatically.

Grims



[View Quote]

lanezeri

Oct 18, 2001, 11:44pm
Grim.. I respect your programming abilities and so I ask you this directly
(although anyone is more than welcome to answer)..

Is this how it would work for the Append instead of Input?

Dim LineText as String
Open App.Path & "\myfile.txt" For Append As #1
' Opens File
Do Until EOF(#1)
Line Input #1, Linetext
txtFileData = LineText 'Or txtFileData = txtFileData &
vbNewLine & LineData
Loop
' Puts Data in txtFileData
Close FileNumber
' Used when done editing file? But would have to declare "#1 =
txtFileData" first?

--

Lanezeri
Lead Bot Programmer at Stuff-X
http://aw.stuff-x.com


[View Quote]

baron

Oct 19, 2001, 3:02am
I didn't really understand what you are trying to do, it would help explaining that first. In general when you open a file For Append you open it to *write* something at the end of the file while preserving the contents, not to read its contents. There is no write or print statement in your code so it won't do anything as is, btw if I remember correctly "Line Input" should be used with the file opened for Input or Binary only. Two obvious errors are:
Do Until EOF(#1) should read Do Until EOF(1)
Close FileNumber should read Close #1


Baron



[View Quote]

grimble

Oct 19, 2001, 10:51am
Just like Baron, I'm not sure what you're trying to do here. I have a nasty
suspicion that you're looking for a way to handle the file for random access
(i.e. inserting, deleting and seeking records withing the contents of a
file). If this is the case then I'm afraid you're out of luck with VB since
the model VB (that damned File System Object!) has no direct support for
random file access as we know it (I was using random access files at school
in Pascal under CPM on an RM-380Z when Windows was just a file manager - I
think M$ believe you should be using databases for random data access
nowadays, although they're not everyone's preference). There is a way to use
random access methods from VB but, in a word, they are crap and don't work
as you would expect (can't delete records ... just blank them out!). If you
want to look at what VB CAN do for random access file, its in the help file
(check in the help index for "random access" or the like) or there are other
resources at development sites around the web, such as
http://pages.about.com/vbmakai/minidb1.htm. It ain't pretty, and, although I
hate to admit it, databases are a much simpler solution - like the Jet DB
Engine through good old DAO.

The Append file mode I mentioned is a replacement for the Output file mode
in VB for adding data to the end of an existing file. You can relate the
Output and Append file modes directly to DOS pipes ">" (write screen output
to file) and ">>" (append screen output to file) respectively. If you open a
file for Output, then any existing file is scrapped and the file is written
afresh. Append will open the existing contents of the file and write any
output to the end of the that file, preserving the contents. I've attached a
small project to demonstrate these two output modes which I hope will
clarify things for you. If not, post back here or e-mail me.

If you can be more specific, explain what you are trying to do, because
there is a lot of programming experience in these newsgroups and there will
certainly be a number of different methods of achieving what you want to do.
If all you want to do is add to the end of the data in a file, then Append
is your boy, but you can't read from a file opened for Output or Append.

Cheers,

Grims


[View Quote]
begin 666 File Mode Demo.zip
M4$L#!!0````(`,9K4RL(J?7&A0,``$$+```+````9G)M1&5M;RYF<FW%55UO
MXS80? ^0_[#0`76"' at S92>[:HGEP_'$UX%B&[5P>`TJD8^(D4I"HQOWW79*B
M12NVD:(H*D"1HQTN9W9VQ>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QNS>,#LT%E-X[ at <UCZ5=^TE[9K8Y-&Q>]USCSQ';]-W^_FC_
M3EJY/YD:)_]G5^SA=]J5Z&E],+MG7'DNN&)'?!'L[:PA-87_SI#6=/T-4$L#
M!!0````(`/QK4RMOIZ\*F0$``(H"```2````1FEL92!-;V1E($1E;6\N=F)P
M55)-CYLP$+TC\1\0N;0K-:)DU4,E#EM86J2$H,*FAZ8'`\/&6W\ at >\ at F6_6_
MU\9TJW(P,^_-Y[.;ZPC)_05\+Y>*)X/B&7"Y-G_?^PH#*! =)#?'S[^B*(JC
MVTWTSAC+D?ZUW'?[X?<J7D>K:)5^/'XKRK(YUE>-P#?Q46,O&<1K9.UJO[T/
M[B:4G""5PO=J) JG,0F7[J'OI9)S(OI-G(3&*PF'),PI at YWL80GY`FQ,I4"X
M8)$E8>2R1E.S=7$.VY$GJ0Z at DO?&IL+9D5WN3+5IO[AVGD)T"C at (7+ :U!E4
M/8VC5&B[:XL:TN;-O<35C;8E"(JP(*,FW;(ZV%).$?IE*LKF71NKMJFQ']'0
M+_]A.3E+59GN=.*5DF^0O[5P:C8Y4'C.H)T>"S%("Y;RCE&BJ7BTWB<YB5ZG
M)^A^SL7-T .3SZ] SBI)!?[SL^+PZCP(9=.ASZMY9WL7LWHS1Q#!DDXQ)%0X
MNSDI('T%:M\^08<6VI%+.?'60(-CM97<][[OZJ!11&C2V74#I^H/)[EY8 at KT
MR4;^`5!+`P04````" `0;%,K+0<,$BL````T````$ at ```$9I;&4 at 36]D92!$
M96UO+G9B=TLKRG5)S<U7L%4P,=$!8S,3"QT%8S,#'05G'04C(P at V,S(#"AH#
M99QYN0!02P$"% `4````" #&:U,K"*GUQH4#``!!"P``"P`````````!`" `
MMH$`````9G)M1&5M;RYF<FU02P$"% `4````" #\:U,K;Z>O"ID!``"*` at ``
M$ at `````````!`" `MH&N`P``1FEL92!-;V1E($1E;6\N=F)P4$L!`A0`% ``
M`` at `$&Q3*RT'#!(K````- ```!(``````````0` at `+:!=P4``$9I;&4 at 36]D
at 92!$96UO+G9B=U!+!08``````P`#`+D```#2!0``````
`
end

lanezeri

Oct 19, 2001, 2:15pm
I'm trying to open a file, get it's contents in a textbox, edit it, and
close the edited file..

I tried both ways I had and they were kind of stupid I know.. I never really
took the time to learn how to do those things.. and my friend has my VB for
Dummies (me being the dummie) book so I couldn't look it up.. sorry for my
ignorance, I know it's quite annoying for me to post something that makes
sense.. any help you could give me would be appreciated..

--

Lanezeri
Lead Bot Programmer at Stuff-X
http://aw.stuff-x.com


[View Quote]

grimble

Oct 19, 2001, 3:20pm
Ok, for what you are trying to do, the principle is really simple ... just
load the file into the edit box and then, when you want to save it again,
just overwrite the whole file with the new contents of the edit box (open
for output, print the lines to the file and close it).

If you are using a multi-line edit box then you just write the entire
contents of the edit box with a single statement. When loading it in,
however, every line will be treated as a single record. So the following is
what you want (txtFile is the name of the edit box on the form and
DEMO_FILE_NAME is a constant defined for the file being used).

'## Load text file into a multi-line edit box
Private Sub LoadFileToForm()

Dim fileNo As Long
Dim fileRec As String
Dim fileData As String
Dim recNo As Long

fileNo = FreeFile
fileData = vbNullString
recNo = 0

'Open the file for reading
Open DEMO_FILE_NAME For Input As #fileNo '<== OPEN FOR INPUT

Do While Not EOF(fileNo)
recNo = recNo + 1

' Decide whether you need to add a carriage return in the string (only
records 2 and onwards)
If Not (recNo = 1) Then
fileData = fileData & vbCrLf

End If

'Read the next record from the file and add it to the end of the
previously loaded content
Line Input #fileNo, fileRec
fileData = fileData & fileRec

Loop

Close #fileNo

'Store the results of the load in the exit box
txtFile.Text = fileData

End Sub


'## Save the contents of the edit box to file
Private Sub SaveFormToFile()

Dim fileNo As Long

fileNo = FreeFile
Open DEMO_FILE_NAME For Output As #fileNo '<== OPEN FOR OUTPUT

'Output the entire contents of the edit box to the file (is done in one
go)
Print #fileNo, txtFile.Text

Close #fileNo

End Sub


Should be enough to be going on with. HTH,

Grims



[View Quote]

baron

Oct 19, 2001, 3:34pm
Ah ok, that's better. Open a new form, add a textbox (txtFileData, make sure its multiline property is set to true) and two command buttons (cmdRead and cmdSave).

Option Explicit ' *Always* a good idea, keeps you in line.
--------------------------------------------
Private Sub cmdRead_Click()
Dim LineText As String
Dim TotalText As String
On Error GoTo hell

'You open the file for input so you can read the contents
Open App.Path & "\myfile.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, LineText 'Read the contents line by line
TotalText = TotalText & LineText & vbCrLf 'and add them as you read them in a string variable
Loop
Close #1
txtFileData.Text = TotalText 'After you read the entire file the variable is added to the textbox

Exit Sub 'Get out so you won't enter the error handler
hell:
'If file doesn't exist it will raise an error, handle it. Pretty primitive but should do for this case
MsgBox "An error occured while attemping to open " & App.Path & "\myfile.txt", _
vbCritical, "Error opening file"
End Sub
---------------------------------------------
Private Sub cmdSave_Click()
'Since you have your entire file in the textbox just overwrite the old one, no need to append
Open App.Path & "\myfile.txt" For Output As #2
Print #2, txtFileData.Text
Close #2
End Sub
===========================================

If you plan a big project with a lot of files opening and closing it's a good idea to use FreeFile instead of hardcoded file handles.
Note that the textbox has filesize limitations. You might want to use RichTextBox if you plan to handle files larger than 64KB or if you want to do some fancy editing using RTF. On the RichTextBox things are easier since it has two methods that automate the above RichTextBox.LoadFile and RichTextBox.SaveFile

HTH
Baron


[View Quote]

lanezeri

Oct 19, 2001, 4:20pm
Many thanks to both of you!

--

Lanezeri
Lead Bot Programmer at Stuff-X
http://aw.stuff-x.com


[View Quote]

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn