2011-11-17 57 views
1

我在網上搜索了高和低,我無法找到一個直接的答案! 我有一個文件,在一個長行中大約有100,000個字符。從字符串創建固定寬度的文件

我需要讀取此文件並將其全部寫出,第102行以VbCrLf結尾的字符。沒有分隔符。

我認爲有很多方法可以解決VB腳本這樣的問題......但是 顯然不是!

任何人都可以請給我一個指針嗎?

回答

0

我不會覆蓋文件的閱讀,因爲那是你可以在任何地方找到的東西。而且由於我已經用vb或vbscript編寫了幾年,我希望.net代碼就足夠了。

僞:從文件中讀取行,將其放入例如一個字符串(性能問題任何人?)。 一個簡單的算法是,這可能有性能問題(多線程,並行可能是一個解決方案):

Public Sub foo() 
    Dim strLine As String = "foo²" 
    Dim strLines As List(Of String) = New List(Of String) 
    Dim nrChars = strLine.ToCharArray.Count 
    Dim iterations = nrChars/102 

    For i As Integer = 0 To iterations - 1 
     strLines.Add(strLine.Substring(0, 102)) 
     strLine = strLine.Substring(103) 
    Next 

    'save it to file 
End Sub 
1

這裏的東西(從我的頭頂 - 未經測試!),應該讓你開始。

Const ForReading = 1 
Const ForWriting = 2 
Dim sNewLine 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set tsIn = fso.OpenTextFile("OldFile.txt", ForReading) ' Your input file 
Set tsOut = fso.OpenTextFile("NewFile.txt", ForWriting) ' New (output) file 

While Not tsIn.AtEndOfStream    ' While there is still text 
    sNewLine = tsIn.Read(102)    ' Read 120 characters 
    tsOut.Write sNewLine & vbCrLf   ' Write out to new file + CR/LF 
Wend         ' Loop to repeat 

tsIn.Close 
tsOut.Close