Results 1 to 7 of 7

Thread: [RESOLVED] change file's content format????

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Resolved [RESOLVED] change file's content format????

    i want to change the context in "sample.txt" in format like "output.txt".

    How to write the code?
    Attached Files Attached Files

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: change file's content format????

    That file is in Unix format (vbLf means newline). You need to convert it to Windows format (vbCrLf means newline.)

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim F1 As Integer
    5. Dim F2 As Integer
    6. Dim length As Long
    7. Dim strText As String
    8.        
    9.     ' Read whole file -->
    10.     F1 = FreeFile
    11.     Open "C:\Sample.txt" For Input As #F1
    12.     length = LOF(F1) 'LOF function returns length of file
    13.     strText = Input$(length, #F1)
    14.     Close #F1
    15.     '
    16.     ' Convert to Windows format -->
    17.     strText = Replace$(strText, vbLf, vbNewLine) 'using vbNewLine is better than using vbCrLf
    18.     '
    19.     ' Save the output -->
    20.     F2 = FreeFile
    21.     Open "C:\Output.txt" For Output As #F2
    22.     Print #F2, strText
    23.     Close #F2
    24. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: change file's content format????

    You have to replace vbLf with vbCrLf:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim intFF As Integer
        Dim strReaded() As String
            intFF = FreeFile
                Open "c:\sample.txt" For Input As #intFF
                    strReaded = Split(Input(LOF(intFF), #intFF), vbLf)
                Close #intFF
            intFF = FreeFile
                Open "c:\output.txt" For Output As #intFF
                    Print #intFF, Join(strReaded, vbCrLf);
                Close #intFF
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: change file's content format????

    Quote Originally Posted by iPrank
    That file is in Unix format (vbLf means newline). You need to convert it to Windows format (vbCrLf means newline.)
    what is "Unix format"? what difference between Unix format and windows format? Can anybody provide me more information about them.Thank you.

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: change file's content format????

    The format of Windows and Unix text files differs slightly.
    In Windows, lines end with both carriage-return (vbCr) and the line-feed (vbLf) ASCII characters. Actually there is a constant, vbCrLf, for this.

    The Apple Macintosh, uses a carriage-return character only.

    And finally Unix uses only a line feed.

    As a consequence, some Windows applications will not show the line breaks in Unix-format/mac-format files. Likewise, Unix/Mac programs may display Windows text files incorrectly.

    So when you open a text file saved in other format, you'll need to convert it in Windows format.

    It is better to use the vbNewLine constant instead of vbCrLf:
    http://www.vbforums.com/showpost.php...30&postcount=8
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: change file's content format????

    What is carriage-return (vbCr) and the line-feed (vbLf)? what diference between them.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: change file's content format????

    vbCr or Carriage Retun is an "ASCII control character" with a value of 13.

    vbLf or Line Feed is is an "ASCII control character" with a value of 10.

    (these terms came from TypeWriter era. Read the linked pages and you'll understand.)

    vbCrLf is just a combination of both constants.

    Open VB. Press F2 to bring Object Browser and then search for these constants. They are defined in VBA.Constants enumaration.

    Also in code window place the text cursor over them and press F1 to bring up MSDN documentation of those constants.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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