CoderZone.org

Category: >> ASP/VB Code >> Reading and Writing to text files in VB Bookmark and Share

<< lastnext >>

Snippet Name: Reading and Writing to text files in VB

Description: Very easy examples of how to open a file and read it, and how to open a file and write to it.

Also see:
» Write or update an htpasswd file i...
» Validate file extensions in upload...
» Javascript Include - allows dynami...
» Remove empty lines from a string o...
» Automatically find location of jav...
» Remove empty lines from text files
» Delete a file in Visual Basic
» Copy a file in VB
» Parsing PHP in Javascript Files
» Perl version of file_get_contents&...
» Read the contents of an entire fil...
» Create an array from the lines of ...
» Remove Duplicate Lines from a File
» Extract Just The BODY Of An HTML F...
» Rename Files Interactively
» Sort A File List By Month Name In ...
» Get File Size Over HTTP
» Get A Directory Listing Of Specifi...
» Check If A Directory Allows File C...
» Copy Files By Date Modified
» Output To STDOUT Or A File
» Include All Files In A Directory
» More File Type Detection in PHP
» Copy a remote file to your site
» Use htpasswd file with PHPscripts
» TABLESPACE: List tablespaces, fil...
» TABLESPACE: Tempfile operations
» Get file extension
» Delete files older than 'X'

Comment: (none)

Author:
Language: VB
Highlight Mode: VB
Last Modified: December 09th, 2010

' a simple example of reading a file:
Dim myFile as String
Dim fileNum as Integer
fileNum = FreeFile
 
'now open the file for input (reading)
Open "C:\testfile.txt" For Input As #fileNum
'change this filename to an existing file! (or run the example below first)
 
' here read the file sequentially until reaching the end
Do While Not EOF(fileNum)
Input #fileNum, myFile
'if you want to show the text, do something like this...
MsgBox myFile
Loop
 
' ALWAYS close the file -if you don't close it, 
' you won't be able to open it again.
Close #fileNum
 
 
' a simple example of writing a file:
Dim myFile as String
Dim fileNum as Integer
fileNum = FreeFile
 
' now open the file for output (writing)
' note that if the file already exists, it will be overwritten
Open "C:\testfile.txt" For Output As #fileNum
 
 
' write some text into the file
Print #fileNum, "Welcome to CoderZone.org "
Print #fileNum, " please feel free to join us in the forums"
' an empty string will create a blank line..
Print #fileNum, "" 
Print #fileNum, " Have a nice day..."
 
' ALWAYS close the file -if you don't close it, 
' you won't be able to open it again.
Close #fileNum
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8