I want to write info to .dat files but i can't write more then 1 line while i want to write something like this:
Lokaal 251
10.42.251.1
10.42.251.2
10.42.251.3
10.42.251.4
And i want to read it line by line (just like .ini)
how do i do this ???
Printable View
I want to write info to .dat files but i can't write more then 1 line while i want to write something like this:
Lokaal 251
10.42.251.1
10.42.251.2
10.42.251.3
10.42.251.4
And i want to read it line by line (just like .ini)
how do i do this ???
I will be told otherwise here (I'm sure there's a proper way someone will point out) but I like to cheat.
1) Set the .dat files attributes to normal :
2) Rename the file to .iniCode:SetAttr "C:\file.dat", VBNormal
3) Carry out the ammendment as normalCode:NAME "C:\file.dat" as "C:\file.ini"
4) Change it back after.
This will work, watch the number of other posts to critisise this & show you a proper way though, come on where are they ? ;)
Read the file:
Code:'Read File
Dim iFileNummer As Integer
Dim sTemp As String
iFileNummer = FreeFile
Open "C:\Lokaal" For Input As #iFileNummer
Do Until EOF(iFileNummer)
Line Input #iFileNummer, sTemp
MsgBox sTemp 'Do some stuff with the contents of the line
Loop
Close #iFileNummer
'End read file
Write to the file
Code:'Write File
Dim iFileNummer As Integer
Dim sTemp As String
iFileNummer = FreeFile
'Output erases the current contents of the file
'Use append if you want to add contents instead of creating a new file
Open "C:\Lokaal" For Output As #iFileNummer
Print #iFileNummer, "Line 1"
Print #iFileNummer, "Line 2"
Print #iFileNummer, "Line 3"
Print #iFileNummer, "Line 4"
Close #iFileNummer
well the easier way is to use the filesystemobject with the text stream object. it would look like this
there are other methods the textstream object has like,Code:dim fsoSystem as New FileSystemObject
dim tsmFile as TextStream
dim strText as String
set tsmFile = fsoSystem.OpenTextFile("c:\testfile.dat", ForAppending)
strText = tsmFile.ReadLine
tsmFile.WriteLine(strText)
readall,skipline,write and others.
it makes writing and reading files very easy and clean.
you don't need to worry about changing the filename and forgetting to change it back or changing the file attributes
what alex shows you isn't cheating, its sloppy code that just has potential to cause yourself problems in the future
no offense alex...
just use the feature vb gives you.. that why it one of the most widely used programming languages....
and if your wondering what to reference to get the filesystemobject and the textstream object.
go into your refrences dialog and add
Microsoft Scripting Runtime (scrrun.dll)
[Edited by ender_pete on 11-21-2000 at 08:37 AM]
None taken, I have made a note of that for me to use in future too.