PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Text File IO  (Read 751 times)

James Fuller

  • Senior Member
  • ***
  • Posts: 309
  • FF3 User
Text File IO
« on: December 20, 2020, 04:37:32 PM »

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.

Thanks

James

Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Text File IO
« Reply #1 on: December 20, 2020, 08:10:47 PM »

SeaVipe

  • Senior Member
  • ***
  • Posts: 397
  • Windows 10
Re: Text File IO
« Reply #2 on: December 20, 2020, 08:26:43 PM »

Hi James, this type of code serves my purposes:
Code: [Select]

' 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

However, Jose's class will do a much better job!
Logged
Clive Richey
You're Either Part Of The Solution, Or You're The Problem.
#StaySafe, Always  Keep A Safe #SocialDistance, #WashYourHands Often and please, #StayHome!

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Text File IO
« Reply #3 on: December 20, 2020, 10:27:25 PM »

CTextStream works with ansi or unicode sequential files.
https://github.com/JoseRoca/WinFBX/blob/master/docs/File%20Management/CTextStream%20Class.md

CFileStream works with binary files.
https://github.com/JoseRoca/WinFBX/blob/master/docs/File%20Management/CFileStream%20Class.md

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

James Fuller

  • Senior Member
  • ***
  • Posts: 309
  • FF3 User
Re: Text File IO
« Reply #4 on: December 21, 2020, 06:20:31 AM »

Josť,
  This is the example from your help with an added else and it fails for me.
James

Code: [Select]
#define UNICODE
#INCLUDE ONCE "windows.bi"
#include "Afx/CTextStream.inc"
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
   pTxtStm.Close
Else
    ? "No GO"   
END IF

PRINT "Press any key to end..."
SLEEP
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9387
  • Windows 10
    • PlanetSquires Software
Re: Text File IO
« Reply #5 on: December 21, 2020, 08:07:18 AM »

For all my programs, I changed the file handling to use Jose's CFileStream and CTextStream. Works perfectly with ansi and unicode and the syntax is simple as well.
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

James Fuller

  • Senior Member
  • ***
  • Posts: 309
  • FF3 User
Re: Text File IO
« Reply #6 on: December 21, 2020, 09:03:11 AM »

For all my programs, I changed the file handling to use Jose's CFileStream and CTextStream. Works perfectly with ansi and unicode and the syntax is simple as well.
Paul,
  Would you provide a small example of writing and reading a unicode text file?
Thank you,
James
Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Text File IO
« Reply #7 on: December 21, 2020, 01:11:12 PM »

Josť,
  This is the example from your help with an added else and it fails for me.
James

The method has an optional parameter to create the file if it does not exist. Therefore, use:

Code: [Select]
IF pTxtStm.OpenForOutputW(ExePath & "\TestW.txt", TRUE) = S_OK THEN
« Last Edit: December 21, 2020, 01:13:47 PM by Josť Roca »
Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Text File IO
« Reply #8 on: December 21, 2020, 01:23:28 PM »

> Would you provide a small example of writing and reading a unicode text file?

In the documentation there are examples to write to a file under the Create, Open (and its variations),  Write, WriteLine,WriteBlankLines topics, and for reading under the ReadLine and ReadAll topics.