Support Forums > WinFBX - Windows Framework for FreeBASIC

Text File IO

(1/2) > >>

James Fuller:
The WinFBX framework is all unicode.
Are there any examples using File IO ?
I find the FreeBasic Help File Open statement a bit confusing with no unicode example.



Josť Roca:
Yes. See the documentation for the CTextStream class:

Hi James, this type of code serves my purposes:

--- Code: ---
' Put and Get return 0 on success.
Dim As Long ff = FreeFile
Dim as Long o = Open( "C:\MyFile.dat" For Binary Access Read as #ff )
If o = 0 Then '' Good Open
'' variable to hold file data
Dim buffer as String
If LOF(ff) > 0 Then
'' our string has as many characters as the file has in bytes
buffer = String(LOF(ff), 0)
'' size of buffer is known. Entire string filled with file data
Get #ff, , buffer
End If   
'' Or
'' Put #ff, 1, buffer
'' I typically don't check the return value of Put or Get
Dim As Long c = Close(#ff) '' or Close(ff)
If c <> 0 Then
? "C:\MyFile.dat. Close Error: "; c
End If
Else '' Bad Open
? "C:\MyFile.dat. open error: "; o
End If

--- End code ---

However, Jose's class will do a much better job!

Josť Roca:
CTextStream works with ansi or unicode sequential files.

CFileStream works with binary files.

And, contrarily to FreeBasic Open function, they also accept unicode for the file names.

James Fuller:
  This is the example from your help with an added else and it fails for me.

--- Code: ---#define UNICODE
#include "Afx/"
using Afx

' // Create an instance of the CTextStream class
DIM pTxtStm AS CTextStream
' // Open file for output as a text stream
IF pTxtStm.OpenForOutputW(ExePath & "\TestW.txt") = S_OK THEN
   ' // Write a string and an end of line to the stream
   pTxtStm.WriteLine "This is a test."
   '// Close the file
    ? "No GO"   

PRINT "Press any key to end..."

--- End code ---


[0] Message Index

[#] Next page

Go to full version