2013-10-06 38 views
1

我有一個VB6程序,保存在一個文本框中的文本到一個文件,當你再次打開它,同樣的文本將在那裏,但每當我重新打開它文本框文本現在有引號,如何刪除引號?代碼:VB6行情出現,只要你打開應用程序

Private Sub Form_Load() 
On Error GoTo NoFile 
Randomize 

Dim sFile As String 
Dim Blank As String 
Dim c1Path As String 
Dim iFileNum As Integer 

sFile = "C:\JPLData" 

iFileNum = FreeFile 
Open sFile For Input As iFileNum 

Line Input #iFileNum, c1Path 
Close #iFileNum 
Text1.Text = c1Path 

NoFile: 
If Err.Number = 5 Then 
sFile = "C:\JPLData" 
c1Path = "No Custom Defined." 

iFileNum = FreeFile 
Open sFile For Output As iFileNum 
Write #iFileNum, Text1.Text 
Close #iFileNum 
End If 


End Sub 

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
Dim sFile As String 
Dim cName As String 
Dim iFileNum As Integer 

sFile = "C:\JPLData" 
cName = vbClrf & Text1.Text & vbClrf 

iFileNum = FreeFile 
Open sFile For Output As iFileNum 
Write #iFileNum, cName 
Close #iFileNum 

End Sub 

編輯: 我已經回答了我自己的問題,我 拼寫vbCrLf錯誤的,我foorgot在BLsnk變量添加到辦理行情:P

回答

0

[Line] InputWrite命令是相當陳舊的陳述,仍然保留了大量的遺留行爲。很可能其中之一是添加引號。

這簡單的解決辦法是probaly只是當顯示的文本框中刪除引號。因此,改變這一行:

Text1.Text = c1Path 

這樣:

Text1.Text = Replace(c1Path, Chr(34), "") 

Chr(34)是引號字符時,Replace函數只是searchs的c1Path字符串的Chr(34)(引號)的所有實例,並替換它們什麼也沒有("",這是空的字符串),有效地刪除它們。

2

documentation表示使用Write #語句編寫的數據通常使用Input #語句讀取。

另外:使用Print #聲明書寫的數據我們通常使用Line Input #聲明讀取。

你混合Write #Line Input #,因此不一致。

0

LINE INPUT從您的文件包括逗號和引號讀取整個文本行。使用LINE INPUT你可以閱讀這樣的CSV數據樣本爲一個字符串變量:

12345,"John Doe","Fake Street",123,"Test Town",900343 

整條生產線將成爲一個字符串,你必須把它拆你自己。 LINE INPUT的「相反」聲明是PRINTPRINT輸出爲字符串爲文件。該字符串可能包含從一個單一的線逗號,引號等

INPUT用於讀取單獨的字段(列值=細胞!)。它預計產生的格式爲WRITE。所以INPUTWRITE一起去。使用INPUT,您可以從文件的單個逗號分隔行讀取多個變量,而無需自行拆分列。您不必使用split或任何正則表達式的東西。而且,你不必做任何類型的投射。 WRITEINPUT只是攜手合作。

例如,一個可以讀取逗號分隔的數據行是這樣的:

Dim Id As Integer, FullName As String, Street As String, Income As Double 
... 
' Write a few separated fields in one operation: 
Write #fx, Id, FullName, Street, Income 
... 
' Read all the fields with different types using just one line of code: 
Input #fy, Id, FullName, Street, Income ' Read the cell values straight into your variables 

如果混合使用兩對LINE INPUT/PRINTINPUT/WRITE你不會得到在大多數情況下,預期的結果。

如果你只想保存/恢復一個單獨的字符串(無論逗號,......它可能包含),並沒有幾個領域(尤其是不同的數據類型(整數,字符串,...)),去LINE INPUTPRINT。那些不包括任何現場分離處理。剛剛與vbCrLf分離整行工作。如果你使用PRINT,你不需要寫任何額外的vbCrLf字符PRINT將自動包括在最後一個換行符。 (如果你不希望在年底該行突破,把;在你的聲明行的末尾。)

' Save a single String 
Dim ff As Integer, MyString As String 
ff = FreeFile 
MyString = "Hello World!" 
Open "myfile.txt" For Output As #ff 
Print #ff, MyString 'Just write the String without any additional characters. 
Close #ff 

' ... Later restore the entire line into one String: 
Dim RestoredString As String 
ff = FreeFile 
Open "myfile.txt" For Input As #ff 
Line Input #ff, RestoredString 
Close #ff 
0

要刪除字符串引號書面方式將文件打印使用,而不是寫的時候。

Dim ff As Integer, MyString As String 
ff = FreeFile 
TheString= "test" 
Open "myfile.txt" For Output As #ff 
Print #ff, TheString 
Close #ff 
相關問題