Results 1 to 5 of 5

Thread: reading and writing to *.dat files

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    13

    Question

    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 ???

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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 :
    Code:
    SetAttr "C:\file.dat", VBNormal
    2) Rename the file to .ini
    Code:
    NAME "C:\file.dat" as "C:\file.ini"
    3) Carry out the ammendment as normal
    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 ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Guest
    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

  4. #4
    Addicted Member ender_pete's Avatar
    Join Date
    Aug 2000
    Location
    Virginia, United States
    Posts
    131
    well the easier way is to use the filesystemobject with the text stream object. it would look like this

    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)
    there are other methods the textstream object has like,
    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]
    ender_pete
    C#,VS.NET Ent Arch, vb6 ee sp5,html,vbscript,jscript,
    xml,dhtml,delphi,c++,vc++,java,cgi,php, python, ada(so ancient) ,adasage(also ancient) and others i can't remember.....

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    None taken, I have made a note of that for me to use in future too.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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