2009-09-10 70 views
7

Tôi muốn mở tệp sai, sao chép tất cả dữ liệu và ghi vào tệp văn bản.Cách đọc tệp và ghi vào tệp văn bản?

Tệp sai của tôi.

File name - 1.mis

M3;3395;44;0;1;;20090404;094144;8193;3;0;;;; 
M3;3397;155;0;2;;20090404;105941;8193;3;0;;;; 
M3;3396;160;0;1;;20090404;100825;8193;3;0;;;; 
M3;3398;168;0;2;;20090404;110106;8193;3;0;;;; 

vân vân ...,

Các dữ liệu trên sẽ xuất hiện trong một tập tin văn bản với tên cùng một tập tin (1.txt).

Tôi đã thử mã này.

Dim sFileText As String 
Dim iFileNo As Integer 
iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
Input #iFileNo, sFileText 
Loop 
Close #iFileNo 

Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
Do While Not EOF(iFileNo) 
Write #iFileNo, sFileText 
Loop 
Close #iFileNo 

Không có gì được lưu trong 1.txt.

+1

Vâng, nếu mis bạn nộp lưu trữ dữ liệu của nó dưới dạng văn bản, bạn chỉ có thể sao chép các tập tin vào 1.txt ... :-) –

+2

Không có gì trong 1.txt là bởi vì bạn đang viết để 2.txt ... –

Trả lời

13

Nó dễ dàng hơn để sử dụng thời gian chạy kịch bản được cài đặt theo mặc định trên Windows

Chỉ cần tham khảo dự án Tham khảo và kiểm tra Thời gian chạy Microsoft Scripting và nhấp OK.

Sau đó, bạn có thể sử dụng mã này đó là cách tốt hơn so với các tập tin mặc định lệnh

Dim FSO As FileSystemObject 
Dim TS As TextStream 
Dim TempS As String 
Dim Final As String 
Set FSO = New FileSystemObject 
Set TS = FSO.OpenTextFile("C:\Clients\Converter\Clockings.mis", ForReading) 
'Use this for reading everything in one shot 
Final = TS.ReadAll 
'OR use this if you need to process each line 
Do Until TS.AtEndOfStream 
    TempS = TS.ReadLine 
    Final = Final & TempS & vbCrLf 
Loop 
TS.Close 

Set TS = FSO.OpenTextFile("C:\Clients\Converter\2.txt", ForWriting, True) 
    TS.Write Final 
TS.Close 
Set TS = Nothing 
Set FSO = Nothing 

Đối với những gì là sai với mã ban đầu của bạn ở đây bạn đang đọc từng dòng của file văn bản.

Input #iFileNo, sFileText 

Sau đó, ở đây bạn viết nó ra

Write #iFileNo, sFileText 

sFileText là một biến chuỗi vì vậy những gì đang xảy ra là mỗi lần bạn đọc, bạn chỉ cần thay thế các nội dung của sFileText với nội dung của dòng bạn chỉ đọc thôi.

Vì vậy, khi bạn viết nó ra, tất cả những gì bạn đang viết là dòng cuối cùng bạn đọc, có lẽ là một dòng trống.

Dim sFileText As String 
Dim sFinal as String 
Dim iFileNo As Integer 
iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
    Input #iFileNo, sFileText 
sFinal = sFinal & sFileText & vbCRLF 
Loop 
Close #iFileNo 

iFileNo = FreeFile 'Don't assume the last file number is free to use 
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
Write #iFileNo, sFinal 
Close #iFileNo 

Lưu ý bạn không cần lặp lại để viết. sFinal chứa toàn bộ văn bản của Tệp đã sẵn sàng để được viết tại một lần chụp. Lưu ý rằng đầu vào đọc một LINE tại một thời điểm để mỗi dòng nối vào sFinal cần phải có một CR và LF nối vào cuối để được viết chính xác trên một hệ thống MS Windows. Hệ điều hành khác có thể chỉ cần một LF (Chr $ (10)).

Nếu bạn cần xử lý dữ liệu đến thì bạn cần phải làm một việc như thế này.

Dim sFileText As String 
Dim sFinal as String 
Dim vTemp as Variant 
Dim iFileNo As Integer 
Dim C as Collection 
Dim R as Collection 
Dim I as Long 
Set C = New Collection 
Set R = New Collection 

iFileNo = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iFileNo 
Do While Not EOF(iFileNo) 
    Input #iFileNo, sFileText 
    C.Add sFileText 
Loop 
Close #iFileNo 

For Each vTemp in C 
    Process vTemp 
Next sTemp 

iFileNo = FreeFile 
Open "C:\Clients\Converter\2.txt" For Output As #iFileNo 
For Each vTemp in R 
    Write #iFileNo, vTemp & vbCRLF 
Next sTemp 
Close #iFileNo 
+0

Một số máy tính người dùng không có FileSystemObject (Tôi đã trải nghiệm điều này). Tôi nghĩ rằng các bộ phận CNTT quá hăng hái đôi khi bị chà đạp trên thời gian chạy kịch bản vì sợ vi-rút – MarkJ

+0

Có đó là lý do tại sao bạn nên đưa vào như là một phần của quá trình cài đặt. Sau đó, sysop có thể quyết định có nên tạo ngoại lệ cho ứng dụng hay không. Hoặc làm cho phiên bản của riêng bạn gói hàm gốc hoặc bọc .NET tương đương. Cả hai đều là IMO quá mức. –

2
FileCopy "1.mis", "1.txt" 
+0

Tính năng chụp ảnh đang hoạt động, Giả sử tôi muốn thêm nội dung nào đó vào tệp văn bản đích, Cách tạo mã. – Gopal

2
An example of reading a file: 
Dim sFileText as String 
Dim iFileNo as Integer 
iFileNo = FreeFile 
'open the file for reading 
Open "C:\Test.txt" For Input As #iFileNo 
'change this filename to an existing file! (or run the example below first) 

'read the file until we reach the end 
Do While Not EOF(iFileNo) 
Input #iFileNo, sFileText 
'show the text (you will probably want to replace this line as appropriate to your program!) 
MsgBox sFileText 
Loop 

'close the file (if you dont do this, you wont be able to open it again!) 
Close #iFileNo 
(note: an alternative to Input # is Line Input # , which reads whole lines). 


An example of writing a file: 
Dim sFileText as String 
Dim iFileNo as Integer 
iFileNo = FreeFile 
'open the file for writing 
Open "C:\Test.txt" For Output As #iFileNo 
'please note, if this file already exists it will be overwritten! 

'write some example text to the file 
Print #iFileNo, "first line of text" 
Print #iFileNo, " second line of text" 
Print #iFileNo, "" 'blank line 
Print #iFileNo, "some more text!" 

'close the file (if you dont do this, you wont be able to open it again!) 
Close #iFileNo 

Từ Here

+0

Xem, @Gopal: Trên cùng ** dữ liệu ** sẽ xuất hiện trong một tệp văn bản giống nhau. Anh/cô ấy cần một FileCopy. – adatapost

+0

@Khenu - Trong tệp nguồn có số dòng n, Làm cách nào để tạo mã? – Gopal

3

Nếu bạn muốn làm điều đó từng dòng:

Dim sFileText As String 
Dim iInputFile As Integer, iOutputFile as integer 

iInputFile = FreeFile 
Open "C:\Clients\Converter\Clockings.mis" For Input As #iInputFile 
iOutputFile = FreeFile 
Open "C:\Clients\Converter\2.txt" For Output As #iOutputFile 
Do While Not EOF(iInputFile) 
    Line Input #iInputFile , sFileText 
    ' sFileTextis a single line of the original file 
    ' you can append anything to it before writing to the other file 
    Print #iOutputFile, sFileText 
Loop 
Close #iInputFile 
Close #iOutputFile 
Các vấn đề liên quan