Вы находитесь на странице: 1из 19

Inverse matrix of 2-by-2 matrix, 3-by-3

matrix, 4-by-4 matrix

Inverse matrix of 2 $\times$ 2 matrix

There exists an inverse matrix of A when detA=ad-bc 0, and it is

Inverse matrix of 3 $\times$ 3 matrix

There exists an inverse matrix of A when


detA=a11a22a33+a21a32a13+a31a12a23-a11a32a23-a31a22a13-a21a12a33
0, and it is
Inverse matrix of 4 $\times$ 4 matrix

If

then there exists an inverse matrix of A, and it is

where
Inverse matrix of NxN matrix
From the analogy of the above formulae, the computation time of inverse matrix of NxN
matrix will be O(N3N!). Computing inverse matrix with Gauss-Jordan method, the
method using LU decomposition, and the method using SVD, will take a computation
time of O(N3) (not confident). I will recommend not to use the formula for calculating
inverse matrix of NxN matrix which N >= 4.
Visual Basic Reading/Writing Text Files
Submitted by:
Yorkiebar
Saturday, September 7, 2013 - 09:31
Language:
Visual Basic
Visitors have accessed this post 677 times.

Introduction:
As you can see from the title, this is a tutorial on simply reading and writing Text Files. Let's begin.

Steps of Creation:
Step 1:
This will require one Import which is to enable us to read and write files. The Import is System.IO:

1. Imports System.IO

Step 2:
The design this program is going to need is to have one textbox to contain the file contents, another textbox
to store the current file path, one button to read a file and another to save the file. The naming of these are:
Textbox1 = Textbox containing file contents
Textbox2 = File Path
Button1 = Read
Button2 = Write

I would also recommend setting ReadOnly to True on textbox2 so the user can not edit the file path.

Step 3:
Now, on to the code!
First let's create the read code, double click on the read button to get our click event generated.

1. Class Form1
2. Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
3. Dim fo As New OpenFileDialog
4. fo.RestoreDirectory = True
5. fo.Filter = "txt files (*.txt)|*.txt"
6. fo.FilterIndex = 1
7. fo.ShowDialog()
8. If (fo.FileName = Nothing) Then
9. MsgBox("Error: No path selected")
10. Else
11. TextBox2.Text = fo.FileName
12. Using sr As New streamreader(fo.FileName)
13. TextBox1.Text = sr.readtoend()
14. End Using
15. End If
16. End Sub
17. End Class

As you can see we create a new variable called "fo" which is set to a New OpenFileDialog. Then we set
some properties of this dialog. The RestoreDirectory means whenever it opens the starting folder will be the
last visited directory from any Windows Explorer/Browser you have used. We then set the file types which
can be opened, I only used .txt but you can also set things like .csv, after that we set the default file type to
.txt (my only file type). We then display the dialog box to the user.

After the user selects a file to read we then check whether the file is set to nothing (If they closed the box)
and display an error message if there is no file selected. If there is a file selected we set the path to textbox2
and use a StreamReader with the path set to our selected file and read the file to our textbox1.

Step 4:
That's it for the reading part, now for the writing part:

1. Private Sub Button2_Click(sender As Object, e As EventArgs)


Handles Button2.Click
2. If (TextBox2.Text = Nothing) Then
3. MsgBox("Please read a file first!")
4. Else
5. Dim res = MsgBox("Do you want to save to the same
file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
6. If (res = MsgBoxResult.Yes) Then
7. If
(My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
8. Using sw As New StreamWriter(TextBox2.Text)
9. sw.Write(TextBox1.Text)
10. End Using
11. End If
12. Else
13. Dim fs As New SaveFileDialog
14. fs.RestoreDirectory = True
15. fs.Filter = "txt files (*.txt)|*.txt"
16. fs.FilterIndex = 1
17. fs.ShowDialog()
18. If (fs.FileName = Nothing) Then
19. MsgBox("Error: No path selected")
20. Else
21. Using sw As New StreamWriter(fs.FileName)
22. sw.Write(TextBox1.Text)
23. End Using
24. End If
25. End If
26. End If
27. End Sub
This is basically the same code, but the opposite - obviously. First we check to see if the path is set in
textbox2 from our read script. If there is no path set it means there is no file open so we display a New
SaveFileDialog for them to select a new file to save their text as. If there is a file open we give them the
option to either overwrite the read file, or, create a new file. Whichever one they choose we create a
StreamWriter with the path address of the selected path (Textbox2 or SaveFileDialog.FileName) and write
textbox1 text to it.

Project Complete!
That's it! Below is the source code to the whole project along with a download in the code section, Thank
you!

1. Imports System.IO
2. Class Form1
3. Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
4. Dim fo As New OpenFileDialog
5. fo.RestoreDirectory = True
6. fo.Filter = "txt files (*.txt)|*.txt"
7. fo.FilterIndex = 1
8. fo.ShowDialog()
9. If (fo.FileName = Nothing) Then
10. MsgBox("Error: No path selected")
11. Else
12. TextBox2.Text = fo.FileName
13. Using sr As New streamreader(fo.FileName)
14. TextBox1.Text = sr.readtoend()
15. End Using
16. End If
17. End Sub
18.
19. Private Sub Button2_Click(sender As Object, e As EventArgs)
Handles Button2.Click
20. If (TextBox2.Text = Nothing) Then
21. Dim fs As New SaveFileDialog
22. fs.RestoreDirectory = True
23. fs.Filter = "txt files (*.txt)|*.txt"
24. fs.FilterIndex = 1
25. fs.ShowDialog()
26. If (fs.FileName = Nothing) Then
27. MsgBox("Error: No path selected")
28. Else
29. Using sw As New StreamWriter(fs.FileName)
30. sw.Write(TextBox1.Text)
31. End Using
32. End If
33. Else
34. Dim res = MsgBox("Do you want to save to the same
file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
35. If (res = MsgBoxResult.Yes) Then
36. If
(My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
37. Using sw As New StreamWriter(TextBox2.Text)
38. sw.Write(TextBox1.Text)
39. End Using
40. End If
41. Else
42. Dim fs As New SaveFileDialog
43. fs.RestoreDirectory = True
44. fs.Filter = "txt files (*.txt)|*.txt"
45. fs.FilterIndex = 1
46. fs.ShowDialog()
47. If (fs.FileName = Nothing) Then
48. MsgBox("Error: No path selected")
49. Else
50. Using sw As New StreamWriter(fs.FileName)
51. sw.Write(TextBox1.Text)
52. End Using
53. End If
54. End If
55. End If
56. End Sub
57. End Class
I want to open mis file, copy all the data and write into a text file.

My mis file.

File name 1.mis

M3;3395;44;0;1;;20090404;094144;8193;3;0;;;;

M3;3397;155;0;2;;20090404;105941;8193;3;0;;;;

M3;3396;160;0;1;;20090404;100825;8193;3;0;;;;

M3;3398;168;0;2;;20090404;110106;8193;3;0;;;;

so on...,

The above data should appear in a text file with same file name (1.txt).

I tried this code.

Dim sFileText As String

Dim iFileNo As Integer

iFileNo = FreeFile

Open "C:\Clients\Converter\Clockings.mis" For Input As


#iFileNo

Do While Not EOF(iFileNo)

Input #iFileNo, sFileText

Loop

Close #iFileNo

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo

Do While Not EOF(iFileNo)

Write #iFileNo, sFileText

Loop

Close #iFileNo

Nothing is saved in 1.txt.

file-io vb6
share|improve this question edited Jul 23 '12 at asked Sep 10 '09 at 11:32
10:59
Deanna Gopal
13.9k31346 2,3231566140
Well, if your
mis file stores
it's data as
text, you could
1 just copy the
file to 1.txt... :-)
Wim ten
Brink Sep 10
'09 at 11:39
There's
nothing in 1.txt
because
you're writing
1 to 2.txt...
Wim ten Brink
Sep 10 '09 at
15:12
add comment

4 Answers
active oldest votes
up vote It far easier to use the scripting runtime which is installed by default on Windows
7 down
vote
accepted Just go project Reference and check Microsoft Scripting Runtime and click OK.

Then you can use this code which is way better than the default file commands

Dim FSO As FileSystemObject

Dim TS As TextStream

Dim TempS As String

Dim Final As String

Set FSO = New FileSystemObject

Set TS = FSO.OpenTextFile("C:\Clients\Converter\Clockings.mis",
ForReading)

'Use this for reading everything in one shot

Final = TS.ReadAll

'OR use this if you need to process each line

Do Until TS.AtEndOfStream

TempS = TS.ReadLine
Final = Final & TempS & vbCrLf

Loop

TS.Close

Set TS = FSO.OpenTextFile("C:\Clients\Converter\2.txt", ForWriting,


True)

TS.Write Final

TS.Close

Set TS = Nothing

Set FSO = Nothing

As for what is wrong with your original code here you are reading each line of the text
file.

Input #iFileNo, sFileText

Then here you write it out

Write #iFileNo, sFileText

sFileText is a string variable so what it is happening that each time you read you just
replace the content of sFileText with the content of the line you just read.

So when you go to write it out all you are writing is the last line you read which is
probably a blank line.

Dim sFileText As String

Dim sFinal as String

Dim iFileNo As Integer

iFileNo = FreeFile

Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo

Do While Not EOF(iFileNo)

Input #iFileNo, sFileText

sFinal = sFinal & sFileText & vbCRLF

Loop

Close #iFileNo
iFileNo = FreeFile 'Don't assume the last file number is free to use

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo

Write #iFileNo, sFinal

Close #iFileNo

Note you don't need to do a loop to write. sFinal contains the complete text of the File
ready to be written at one shot. Note that input reads a LINE at a time so each line
appended to sFinal needs to have a CR and LF appended at the end to be written out
correctly on a MS Windows system. Other operating system may just need a LF
(Chr$(10)).

If you need to process the incoming data then you need to do something like this.

Dim sFileText As String

Dim sFinal as String

Dim vTemp as Variant

Dim iFileNo As Integer

Dim C as Collection

Dim R as Collection

Dim I as Long

Set C = New Collection

Set R = New Collection

iFileNo = FreeFile

Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo

Do While Not EOF(iFileNo)

Input #iFileNo, sFileText

C.Add sFileText

Loop

Close #iFileNo

For Each vTemp in C

Process vTemp

Next sTemp
iFileNo = FreeFile

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo

For Each vTemp in R

Write #iFileNo, vTemp & vbCRLF

Next sTemp

Close #iFileNo

share|improve this answer answered Sep 10


'09 at 13:12

RS Conley
6,075826
Some user computers don't have FileSystemObject (I have experienced this). I think
overzealous IT departments sometimes trample on the scripting runtime for fear of viruses
MarkJ Sep 24 '09 at 9:35
Yes which is why you should include as part of your install. The sysop can then decide
whether to make an exception for the application. Or make your own version wrapping the
native function or wrap the .NET equivalent up. Both of which are overkill IMO. RS
Conley Sep 24 '09 at 12:10
add comment

up If you want to do it line by line:


vote 3
down
vote Dim sFileText As String

Dim iInputFile As Integer, iOutputFile as integer

iInputFile = FreeFile

Open "C:\Clients\Converter\Clockings.mis" For Input As #iInputFile

iOutputFile = FreeFile

Open "C:\Clients\Converter\2.txt" For Output As #iOutputFile

Do While Not EOF(iInputFile)

Line Input #iInputFile , sFileText

' sFileTextis a single line of the original file


' you can append anything to it before writing to the other file

Print #iOutputFile, sFileText

Loop

Close #iInputFile

Close #iOutputFile

share|improve this answer edited Jun 14 at answered Sep 10


12:38 '09 at 13:07

C-Pound Guru
5,57021531

add comment
up FileCopy "1.mis", "1.txt"
vote 1
down share|improve this answer answered Sep 10
vote '09 at 11:40

Yossarian
8,1591651
Filecopy is working, Suppose i want to add something in a destination text file, How to make
code. Gopal Sep 10 '09 at 11:57
add comment
up An example of reading a file:
vote 1
down Dim sFileText as String
vote
Dim iFileNo as Integer

iFileNo = FreeFile

'open the file for reading

Open "C:\Test.txt" For Input As #iFileNo

'change this filename to an existing file! (or run the example below
first)

'read the file until we reach the end

Do While Not EOF(iFileNo)

Input #iFileNo, sFileText

'show the text (you will probably want to replace this line as
appropriate to your program!)
MsgBox sFileText

Loop

'close the file (if you dont do this, you wont be able to open it again!)

Close #iFileNo

(note: an alternative to Input # is Line Input # , which reads whole


lines).

An example of writing a file:

Dim sFileText as String

Dim iFileNo as Integer

iFileNo = FreeFile

'open the file for writing

Open "C:\Test.txt" For Output As #iFileNo

'please note, if this file already exists it will be overwritten!

'write some example text to the file

Print #iFileNo, "first line of text"

Print #iFileNo, " second line of text"

Print #iFileNo, "" 'blank line

Print #iFileNo, "some more text!"

'close the file (if you dont do this, you wont be able to open it again!)

Close #iFileNo

From Here

share|improve this answer

Reading From Excel File :-


**************************************************
Dim ExcelApp As Excel.Application
Dim WS As Excel.Worksheet
Dim i As Integer
Dim stra As String
Set ExcelApp = CreateObject("excel.application")
ExcelApp.Workbooks.Open (CommonDialog1.filename)
Set WS = ExcelApp.ActiveWorkbook.Sheets("VendorSheet") ''Sheet
For i = 1 to 10
stra="a"+cstr(i)
Msgbox WS.Range(stra).value
Next i

Above code will read 10 lines and column a of Excel sheet.


Aa1,a2,a3......a10.

for other colums just create the range like "b1", "m10" etc.
Hope u got the point.
************************************************** *****

Reading from text file:-


*************************

'''Add reference to "MicroSoft Scripting runtime" to ur project


then

Dim Fso as new FileSystemObject


Dim FileToread As TextStream
Dim CurrentLine as String

Set FileToread = fso.OpenTextFile("C:\a.txt", ForReading)

Do Whiile FileToread.AtEndOfStream = False


CurrentLine=FileToread.ReadLine
msgbox CurrentLine
Loop

*******************************************

Writing to text file


***********************
Dim FileToWrite as TextStream

Set FileToWrite = Fso.OpenTextFile("C:\mytxtfile", ForWriting, True)

FileToWrite.WriteLine "This is Line 1"


FileToWrite.WriteLine "This is Line 2"

FileToWrite.Close
**************

Hope this helps..Good luck..Happy programming

B. Anant

#6 (permalink)

October 8th, 2004, 04:56 AM


Join Date: Oct 2004
priya75 Location: , , .
Posts: 1
Registered User
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Anant,

I have to read the data from a table in MSWORD. Then I have to execute a macro which takes
these inputs and generates a text file from it.
I am a newbie to VB usage .Can you tell me in steps as to how do
i make the VB script read the data from the MSWord Table.

An early response would be highly appreciated.

Regards
Priya
Quote:
quote:Originally posted by Anantsharma
HI,

Reading From Excel File :-


**************************************************
Dim ExcelApp As Excel.Application
Dim WS As Excel.Worksheet
Dim i As Integer
Dim stra As String

Set ExcelApp = CreateObject("excel.application")


ExcelApp.Workbooks.Open (CommonDialog1.filename)
Set WS = ExcelApp.ActiveWorkbook.Sheets("VendorSheet") ''Sheet
For i = 1 to 10
stra="a"+cstr(i)
Msgbox WS.Range(stra).value
Next i

Above code will read 10 lines and column a of Excel sheet.


Aa1,a2,a3......a10.

for other colums just create the range like "b1", "m10" etc.
Hope u got the point.
************************************************** *****

Reading from text file:-


*************************

'''Add reference to "MicroSoft Scripting runtime" to ur project


then

Dim Fso as new FileSystemObject


Dim FileToread As TextStream
Dim CurrentLine as String

Set FileToread = fso.OpenTextFile("C:\a.txt", ForReading)

Do Whiile FileToread.AtEndOfStream = False


CurrentLine=FileToread.ReadLine
msgbox CurrentLine
Loop

*******************************************

Writing to text file


***********************
Dim FileToWrite as TextStream

Set FileToWrite = Fso.OpenTextFile("C:\mytxtfile", ForWriting, True)

FileToWrite.WriteLine "This is Line 1"


FileToWrite.WriteLine "This is Line 2"

FileToWrite.Close
**************

Hope this helps..Good luck..Happy programming

B. Anant

Creating Text file and insert values using


vb
MdShariq asked Apr 5, 2007 | Replies (5)
Dear All,

I am VB Programmer and struggling to create text file. How to create a


text file using vb and using the same file how to insert values?

Please help me in this regard.

Regards
Md Shariq.
Join this group

Popular White Paper On This Topic


Getting Started with ERP
5 Replies
0

Rolan mahor replied Apr 5, 2007


Hi.. Try this...

Create a text file..


open "Text.txt" for append as #1
print #1, "This is a text file"
close #1

open "c:\Text.txt" for input #1


Do Until EOF(1)
Line Input #1, temp
Loop

To insert into database


POSTRAN.Execute "Insert from table (textfile) VALUES (" _
& " temp & "'," _
& ")"

If you want to concatenate the text file then u will use split.
0

Veena Hebsur replied Apr 5, 2007


Hi,

Use this Code:

Dim FN As Long
Dim sSQL As String

FN = FreeFile
sSQL = "C:\MyText.txt"
Open sSQL For Output As FN
Print #FN, "THIS TEXT FILE CREATED FROM VB"
Print #FN, "CHECK FILE "
Close FN

Instead of "Output" u can use "Append" to insert text in the Same File.
If Output is used, then every time new file is created.

Regards
Veena
0

Santosh Kadam replied Apr 5, 2007


Hi,

Use filesystem object for writing/creating text files.

Regards,
Santosh Kadam
0

User940240 replied Apr 9, 2007


hi,

i have a problem in vb 6 when reading a character in text file (values)


, i want to check in text file if values of character is exist.

thank you,
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
Glenn A. Timbreza
MIS/Acctg. Staff
*NANBU PHILILIPPINES INC.*
eMail: email@removed
Tel: 046 437.2066
Fax: 046 437.2065
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
White Papers and Webcasts
Popular

The Best PDF Features Businesses Don't Use


Related

9 Social Insights that Drive Real Business Results


City of Melrose vCloud Hybrid Service
From Audience to Evangelist: Lifecycle Email Marketing 2.0
More White Papers
0

Allen Feld replied Apr 10, 2007


You can read it in line by line and do an instr:

InStr(1, "Computer Science", "put", vbTextCompare)

will return "4" because the fourth character is the start of "put".
Usually, if I'm checking this, I will use something like

if InStr(1, "Computer Science", "put", vbTextCompare) > 1 then


....

end if

The first value is where you want to start (almost always 1) , the second
is the string you want to search, third is the string you want to look
for, and fourth is the search method.

Вам также может понравиться