CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2001
    Location
    Kenosha, Wi
    Posts
    37

    File Editing and Formatting

    Hello all,

    I have an application that simply takes a formatted text file and replaces a phone number in it. The user just simply adds the phone number and then click on the command button and it reads the file into memory, replaces the string, and then writes it back to file. It does everything Just great!

    My problem is the fact that it cuts ALL my whitespaces and formatting from the file. If this file is off by one whitespace, my decoder will not work at all. My file goes from 9k to 2k because this code cuts out all my white spaces. Anyone have any ideas how I can accomplish my mission while keeping the file format intact? Here is my code:

    Option Explicit

    Private Sub Command1_Click()
    Dim strTextFile As String
    Dim strTextLine As String
    Dim strPhoneNumber As String

    strPhoneNumber = Text1.Text ' Get the phone number

    Open "C:\temp\Test.txt" For Input As #1 ' Open file.
    Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, strTextLine ' Read line into variable.
    strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
    Loop
    Close #1 ' Close file.

    strTextFile = Replace(strTextFile, "<PhoneNumber>", strPhoneNumber) 'adds the public text vatable that the user input overwriting the <PhoneNumber> text

    Open "C:\temp\Test.txt" For Output As #1
    Print #1, strTextFile
    Close #1

    End Sub


    Timothy H. Schilbach
    Alpha Omega Design Inc.
    [email protected]

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: File Editing and Formatting

    TRy:

    Dim strTextFile as string
    Dim strTextLine as string
    strTextFile = "Blabla blabla " & vbCrLf & " ababa 2 ababa ababa "
    Open "c:\txtmy.txt" for Output as #1
    print #1, strTextFile
    Close #1
    strTextFile = ""
    Open "c:\txtmy.txt" for input as #1
    Do While Not EOF(1) ' Loop until end of file.
    Line input #1, strTextLine ' Read line into variable.
    strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
    Loop
    Close #1
    'strip last terminator
    strTextFile = Left$(strTextFile, len(strTextFile) - 2)
    strTextFile = Replace(strTextFile, "2", "024043", , , vbBinaryCompare)
    Open "c:\txtmy.txt" for Output as #1
    print #1, strTextFile
    Close #1




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2001
    Location
    Kenosha, Wi
    Posts
    37

    Re: File Editing and Formatting

    Thank you for the code. I adapted it to my situation and it stille truncated ALL whitespaces out of my file. Would you be interrested in seeing the before and after result of this code? I can zip them and send them too ya.

    Here is the script adaption:

    Option Explicit

    Private Sub Command1_Click()
    Dim strTextFile As String
    Dim strTextLine As String
    Dim strTextInput As String

    strTextInput = Text1.Text

    strTextFile = ""
    Open "c:\temp\dialup\default.set" For Input As #1
    Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, strTextLine ' Read line into variable.
    strTextFile = strTextFile & strTextLine & vbCrLf ' Collect the input file.
    Loop
    Close #1 'strip last terminator

    strTextFile = Left$(strTextFile, Len(strTextFile) - 2)
    strTextFile = Replace(strTextFile, "<PhoneNumber>", strTextInput, , , vbBinaryCompare)

    Open "c:\temp\dialup\default.set" For Output As #1
    Print #1, strTextFile
    Close #1
    End Sub


    Timothy H. Schilbach
    Alpha Omega Design Inc.
    [email protected]

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: let me see

    interrested in seeing the before and after result of this code:
    e-mail it to
    [email protected]
    I will take a look and give you an answer (succesfull, I hope) within tomorrow.
    Cesare

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: This seems to work


    'add a richtextbox control (visible= false) to a form, and try this code.
    'Your matter is : you have a lot of chr$(6) in your text. Look at after file
    'with your application. Hope this may solve.

    private Sub Command1_Click()

    With RichTextBox1
    .FileName = "C:\before.txt"
    'use "<" and ">" as they are readed this way
    .Text = Replace(.Text, "<PhoneNumber>", "02020202", , , vbBinaryCompare)
    .SaveFile "c:\after.txt", rtfText
    'debug:
    '.FileName = "c:\after.txt"
    End With
    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2001
    Location
    Kenosha, Wi
    Posts
    37

    Re: let me see

    Thank you for the help. I never even thought of using that pesky RichTextBox :-)

    Good thing I have the Enterprise Version :-)

    If there is anything I can ever do for you let me know. I will finnish my testing when I go and sdecode the file. I will let you know how it all turns out.


    Timothy H. Schilbach
    Alpha Omega Design Inc.
    [email protected]

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: If there is anything...

    ...Just let me know if it worked in your case...
    Best regards,
    Cesare

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured