VB6在为文本文件添加空格时,如何避免括号内的文本?

时间:2015-06-07 08:55:09

标签: vb6

我想读一个部分没有空格的文本文件,如下所示: (为了便于在这个网页上阅读,我不得不将这些线条间隔开来。 我的问题是“单词”之间的间距,而不是行间距

O0377(NO EXTRA SPACES REQUIRED INSIDE ALL PARENTHESES)
(T1 WNMG R0.8) 
(T2 TNMG R0.8  SKEWED OUT) 
(T3 3MM P/BLADE) 
(T5 16MM U/DRILL CUTTING 16.5) 
(CYCLE TIME 1MIN 5SEC) 
G50S3000 
G30U0W0M8(ROUGH OD AND FACE) 
T0101

(NO EXTRA SPACES REQUIRED INSIDE SQUARE BRACKETS) 
[IF#100GT#101GOTO99]
G96G99S180M3 
G0X48.2Z20.
Z3.
N99G1Z-6.2F0.2
X12.F0.3 
G30W0M24 
M30

然后在每个字母之前添加一个空格,除了在括号内 - 这些已经是间隔的 - 除了方括号内 - 在那里不需要空格。 它应该是这样的:

O0377(NO EXTRA SPACES REQUIRED INSIDE ALL PARENTHESES) 
(T1 WNMG R0.8)
(T2 TNMG R0.8  SKEWED OUT) 
(T3 3MM P/BLADE) 
(T5 16MM U/DRILL CUTTING 16.5) 
(CYCLE TIME 1MIN 5SEC) 
G50 S3000 

G30 U0 W0 M8 (ROUGH OD AND FACE) 

T0101

(NO EXTRA SPACES REQUIRED INSIDE SQUARE BRACKETS) 
[IF#100GT#101 GOTO99]
G96 G99 S180 M3 
G0 X48.2 Z20.
Z3.
N99 G1 Z-6.2 F0.2
X12. F0.3 
G30 W0 M24 
M30

我的问题是我的程序在任何地方放置空格,如下所示:

O0377 ( N O  E X T R A S P A C E S  R E Q U I R E D  I N S I D E  A L L  P A R E N T H E S E S) 
( T1  W N M G  R0.8) 
( T2  T N M G  R0.8   S K E W E D  O U T) 
( T3 3 M M  P/ B L A D E) 
( T5 16 M M  U/ D R I L L  C U T T I N G 16.5) 
( C Y C L E  T I M E 1 M I N 5 S E C) 
G50 S3000 
G30 U0 W0 M8 ( R O U G H  O D  A N D  F A C E) 
T0101

( N O  E X T R A  S P A C E S  R E Q U I R E D  I N S I D E  S Q U A R E  B R A C K E T S) 
[ I F#100 G T#101 G O T O99]
G96 G99 S180 M3 
G0 X48.2 Z20.
Z3.
N99 G1 Z-6.2 F0.2
X12. F0.3 
G30 W0 M24 
M30

这是我的代码。 (不知道如何使它看起来像代码......) 它不漂亮,但我能理解它,并且处理一个典型的文件并不需要很长时间。

Private Sub Form_Load()
Dim InputLines, NextLine, OutputLines As String
Dim fileIn, fileOut As Integer

fileIn = FreeFile
Open "C:\Documents and Settings\Owner\Desktop\FileToBeSpaced.txt" For Input As fileIn

fileOut = FreeFile
Open "C:\Documents and Settings\Owner\Desktop\SpacedFile.txt" For Append As fileOut


Do While Not EOF(fileIn)
    Line Input #fileIn, NextLine
    InputLines = InputLines + NextLine + Chr(13) + Chr(10)

    OutputLines = InputLines
    OutputLines = Replace(OutputLines, "A", " A")
    OutputLines = Replace(OutputLines, "B", " B")
    OutputLines = Replace(OutputLines, "C", " C")
    OutputLines = Replace(OutputLines, "D", " D")
    OutputLines = Replace(OutputLines, "E", " E")
    OutputLines = Replace(OutputLines, "F", " F")
    OutputLines = Replace(OutputLines, "G", " G")
    OutputLines = Replace(OutputLines, "H", " H")
    OutputLines = Replace(OutputLines, "I", " I")
    OutputLines = Replace(OutputLines, "J", " J")
    OutputLines = Replace(OutputLines, "K", " K")
    OutputLines = Replace(OutputLines, "L", " L")
    OutputLines = Replace(OutputLines, "M", " M")
    OutputLines = Replace(OutputLines, "N", " N")
    OutputLines = Replace(OutputLines, "O", " O")
    OutputLines = Replace(OutputLines, "P", " P")
    OutputLines = Replace(OutputLines, "Q", " Q")
    OutputLines = Replace(OutputLines, "R", " R")
    OutputLines = Replace(OutputLines, "S", " S")
    OutputLines = Replace(OutputLines, "T", " T")
    OutputLines = Replace(OutputLines, "U", " U")
    OutputLines = Replace(OutputLines, "V", " V")
    OutputLines = Replace(OutputLines, "W", " W")
    OutputLines = Replace(OutputLines, "X", " X")
    OutputLines = Replace(OutputLines, "Y", " Y")
    OutputLines = Replace(OutputLines, "Z", " Z")
    OutputLines = Replace(OutputLines, "(", " (")

Loop

Print #fileOut, OutputLines

Close fileIn
Close fileOut

Unload frmInsertSpaces

End Sub

“单词”间距是主要问题。我想避免不必要的额外空间。

然后,显然我不想使用固定的路径和文件名,就像我现在的代码中一样。 我想将任何不间隔的文件放到图标或其他东西上,并将其间隔开,并自动保存到桌面。甚至右键单击一个未空格的文件,然后从那里开始。

如果有人能提供(最好是简单的)解决方案,或指出我正确的方向,我将非常感激。提前谢谢,皮特。

1 个答案:

答案 0 :(得分:1)

非常常规的东西。

如果不是你想要的那样,这应该是接近的。尝试遵循代码,该代码使用“内联字符串构建器”技术来避免串联性能瓶颈。需要这样的事情,所以你应该熟悉这种方法,而不仅仅是复制/粘贴代码分发。

由于你的头衔和上面描述的问题描述不好,我对任何未来的读者绝望都会发现这一点,并将其应用于其他问题。

Option Explicit

Private Sub Main()
    Dim InpF As Integer   'Input file number.
    Dim InpText As String 'Input line of text.
    Dim InpP As Long      'Input "pointer" within line.
    Dim NewF As Integer
    Dim NewText As String
    Dim NewP As Long
    Dim Char As String
    Dim EndB As String    'End bracket character.
    Dim TempP As Long
    Dim Length As Long    'Length of bracketed text.

    InpF = FreeFile(0)
    Open "original.txt" For Input As #InpF
    NewF = FreeFile(0)
    Open "new.txt" For Output As #NewF
    Do Until EOF(InpF)
        Line Input #InpF, InpText
        NewText = Space$(Len(InpText) * 3 \ 2) 'Allocate estimated space.
        NewP = 1
        For InpP = 1 To Len(InpText)
            Char = Mid$(InpText, InpP, 1)
            If Char = "(" Or Char = "[" Then
                If Char = "(" Then EndB = ")" Else EndB = "]"
                TempP = InStr(InpP + 1, InpText, EndB)
                If TempP = 0 Then TempP = Len(InpText)
                Length = TempP - InpP + 1
                If Len(NewText) < NewP + Length - 1 Then
                    NewText = NewText & Space$(Length) 'Add more space.
                End If
                Mid$(NewText, NewP, Length) = Mid$(InpText, InpP, Length)
                InpP = InpP + Length
                NewP = NewP + Length
            Else
                If Char Like "[A-Z]" And InpP > 1 Then NewP = NewP + 1
                If Len(NewText) < NewP + 2 Then
                    NewText = NewText & Space$(10) 'Add more space.
                End If
                Mid$(NewText, NewP) = Char
                NewP = NewP + 1
            End If
        Next
        Print #NewF, Left$(NewText, NewP - 1)
    Loop
    Close #NewF
    Close #InpF
End Sub