i want to change the context in "sample.txt" in format like "output.txt".
How to write the code?
Printable View
i want to change the context in "sample.txt" in format like "output.txt".
How to write the code?
That file is in Unix format (vbLf means newline). You need to convert it to Windows format (vbCrLf means newline.)
vb Code:
Option Explicit Private Sub Command1_Click() Dim F1 As Integer Dim F2 As Integer Dim length As Long Dim strText As String ' Read whole file --> F1 = FreeFile Open "C:\Sample.txt" For Input As #F1 length = LOF(F1) 'LOF function returns length of file strText = Input$(length, #F1) Close #F1 ' ' Convert to Windows format --> strText = Replace$(strText, vbLf, vbNewLine) 'using vbNewLine is better than using vbCrLf ' ' Save the output --> F2 = FreeFile Open "C:\Output.txt" For Output As #F2 Print #F2, strText Close #F2 End Sub
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
what is "Unix format"? what difference between Unix format and windows format? Can anybody provide me more information about them.Thank you.Quote:
Originally Posted by iPrank
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
What is carriage-return (vbCr) and the line-feed (vbLf)? what diference between them.
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.