L3DT users' wiki
Large 3D terrain generator

 

Scripts > fread test

Description An example script demonstrating how to use the fopen, fread and fclose script functions.
Author Aaron
Created 2010/05/24
Requires L3DT v2.9 beta 2 or later
Download fread test.zs

Script contents

//
// Ask user for filename
//

string FileName
set FileName <file.OpenDlg "txt" NULL "Text files (*.txt)|*.txt|All files (*.*)|*.*|">
if <iseq 0 <strlen FileName>>
  return 0
endif

//
// Open file
//

voidptr fp
set fp <fopen FileName "r">
assert fp "Cannot open file!"

//
// Prepare some variables
//

char c
string s
bool FileDone
set FileDone false

//
// Read file character-by-character, and add to string
//

do
  if <fread fp c>
    set s <strcat s c>
  else
    set FileDone true
  endif
while <not FileDone>

//
// Write file contents (in string) to event log
//

echo s

//
// Report file length using ftell
//

echo <strcat <strcat "File length was " <ftell fp>> " bytes">

//
// Close file
//

fclose fp

Comments

None.

 
scripts/fread_test.txt · Last modified: 2017/08/31 07:09 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki