请问怎样用这个函数向文本文件写如字符呢,好像用VC的方法是不行的

解决方案 »

  1.   

    要打开现有的文件,则使用 FileSystemObject 对象的 OpenTextFile 方法或 File 对象的 OpenAsTextStream 方法。要写数据到打开的文本文件,则根据下表所述任务使用 TextStream 对象的 Write、WriteLine 或 WriteBlankLines 方法。任务 方法 
    向打开的文本文件写数据,不用后续一个新行字符。 Write 
    向打开的文本文件写数据,后续一个新行字符。 WriteLine 
    向打开的文本文件写一个或多个空白行。 WriteBlankLines 要关闭一个打开的文件,则使用 TextStream 对象的 Close 方法。注意  新行字符包含一个或几个字符(取决于操作系统),以把光标移动到下一行的开始位置(回车/换行)。注意某些字符串末尾可能已经有这个非打印字符了。下面的 VBScript 例子示范了如何打开文件,和同时使用三种写方法来向文件添加数据,然后关闭文件:Sub CreateFile()
       Dim fso, tf
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set tf = fso.CreateTextFile("c:\testfile.txt", True)
       ' 写一行,并且带有新行字符。
       tf.WriteLine("Testing 1, 2, 3.") 
       '向文件写三个新行字符。        
       tf.WriteBlankLines(3) 
       '写一行。
       tf.Write ("This is a test.") 
       tf.Close
      

  2.   

    我不喜欢用FSO的,打开现有文件也可以用CREATEFILE或OPENFILE,但就不能直接用WRITEFILE写文本
    在VC我是这样写的: HFILE hFile;
    DWORD i;
    OFSTRUCT Ofs;
    CString a;
    a="aaa";
    hFile=OpenFile("c:\\yy.txt",&Ofs,OF_CREATE);
    WriteFile((HANDLE)hFile,(LPCVOID)(LPCTSTR)a,a.GetLength(),&i,FALSE);
    改成VB就是:
             Public Sub MyCreateFile()
               Dim hFile As Long
               Dim i As Long
               Dim txt As String
               txt = "aaa"
               i = Len(txt)
               hFile = OpenFile("c:\yy.txt", Ofs, OF_CREATE)
               If hFile <> HFILE_ERROR Then
                 WriteFile hFile, txt, Len(txt), i, Ol
               End If
               CloseHandle hFile
             End Sub
    但就是不行,请问高手我错了哪里呢
      

  3.   

    Option ExplicitPrivate Const OF_CREATE = &H1000
    Private Const OFS_MAXPATHNAME = 128Private Type OFSTRUCT
            cBytes As Byte
            fFixedDisk As Byte
            nErrCode As Integer
            Reserved1 As Integer
            Reserved2 As Integer
            szPathName(OFS_MAXPATHNAME) As Byte
    End TypePrivate Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click()
        MyCreateFile
    End SubPublic Sub MyCreateFile()
        Dim hFile As Long
        Dim i As Long
        Dim txt As String
        Dim ofs As OFSTRUCT
        txt = "你的明白?"
        'txt = "I see"
        txt = StrConv(txt, vbFromUnicode)
        hFile = OpenFile("c:\yy.txt", ofs, OF_CREATE)
        WriteFile hFile, ByVal StrPtr(txt), LenB(txt), i, 0
        CloseHandle hFile
    End Sub