Professional Documents
Culture Documents
Ring Programming Language Book - Part 15 of 84
Ring Programming Language Book - Part 15 of 84
fwrite()
fexists()
Example:
see read("myfile.txt")
Example:
# copy file
cFile = read("ring.exe")
write("ring2.exe",cFile)
We can get the folder contents (files & sub folders) using the Dir() function.
Syntax:
Dir(cFolderPath) ---> List contains files & sub folders.
This function returns a list and each list item is a list of two items
File/sub folder name
Type (0 = File , 1 = Folder/Directory)
Example:
Example:
rename("file.txt","help.txt")
Example:
remove("test.txt")
Mode Description
r Reading (The file must exist)
w Writing (create empty file / overwrite)
a Appends (create file if it doesnt exist)
r+ update (reading/writing)
w+ Create empty file (reading/writing)
a+ reading & appending
When we open a file using fopen() function, we can close it using the Fclose() function
Syntax:
Fclose(file handle)
We can flushes the output buffer of a stream using the Fflush() function
Syntax:
Fflush(file handle)
We can open another file using the same file handle and at the same file close the old file
Syntax:
Freopen(cFileName,cMode,file handle) ---> file handle
Example:
freopen("myprogoutput.txt","w+",stdout)
see "welcome" + nl
for x = 1 to 10
see x + nl
next
/*
** Read : https://en.wikipedia.org/wiki/Device_file#Device_files
** The next code is not portable, we can use iswindows() before
** using it and we can write special code for each operating system.
*/
Output:
# Output to stdout
Done
8
9
10
We can set the file position of the stream using the Fseek() function
Syntax:
Fseek(file handle, nOffset, nWhence) ---> zero if successful
We can know the current file position of a stream using the Ftell() function
Syntax:
Ftell(file handle) ---> file position as number
We can set the file position to the beginning of the file using the Rewind() function
Syntax:
Rewind(file handle)
We can get handle to the current file position using the Fgetpos() function
Syntax:
Fgetpos(file handle) ---> position handle
We can set the current file position using the Fgetpos() function
Syntax:
Fsetpos(file handle,position handle)
We can clear the EOF error and the error indicators of a stream using the clearerr() function
Syntax:
Clearerr(file handle)
We can test the error indicator of a given stream using the Ferror() function
Syntax:
Ferror(file handle) ---> returns 1 if error and 0 if not
We can print error message to the stderr using the Perror() function
Syntax:
Perror(cErrorMessage)
We can get the next character from the stream using the Fgetc() function
Syntax:
Fgetc(file handle) ---> returns character or EOF
We can read new line from the stream using the Fgets() function
Syntax:
Fgets(file handle,nSize) ---> string
The function stop when nSize characters are read, new line character is read or EOF.
Ungetc(file handle,character)
Example:
see fexists("b:\mahmoud\apps\ring\ring.exe") + nl +
fexists("b:\mahmoud\apps\ring\ring2.exe") + nl
Output:
1
0
26.29 Example
See "reopen" + nl
fp = freopen("tests\s78.ring","r",fp)
See "close file" + nl
fclose(fp)
fp = tempfile()
fclose(fp)
remove("tests\mytest1.txt")
write("tests\test1.txt","hello")
rename("tests\test1.txt","tests\mytest2.txt")
fp = fopen("tests\myfile.txt","rw+")
fseek(fp,0,2) # goto end of file
fputc(fp,"t")
fputc(fp,"e")
fputc(fp,"s")
fputc(fp,"t")
fputs(fp,"test2")
fclose(fp)
fp = fopen("tests\myfile.txt","r")
see "testing ungetc() " + nl
for x = 1 to 3
r = fgetc(fp)
see r + nl
ungetc(fp,r)
next
fclose(fp)
TWENTYSEVEN
SYSTEM FUNCTIONS
Example:
System("myapp.exe") # Run myapp.exe
System("ls") # print list of files
128
Ring Documentation, Release 1.2
Example:
see sysget("path") # print system path information
We can check if the operating system is MSDOS or not using the IsMSDOS() function
Syntax:
IsMSDOS() ---> Returns 1 if the operating system is MS-DOS, Returns 0 if it's not
We can check if the operating system is Windows or not using the IsWindows() function
Syntax:
IsWindows() ---> Returns 1 if the operating system is Windows, Returns 0 if it's not
We can check if the operating system is Windows 64bit or not using the IsWindows64() function
Syntax:
IsWindows64() ---> Returns 1 if the operating system is Windows64, Returns 0 if it's not
We can check if the operating system is Unix or not using the IsUnix() function
Syntax:
IsUnix() ---> Returns 1 if the operating system is Unix, Returns 0 if it's not
We can check if the operating system is Mac OS X or not using the IsMacOSX() function
Syntax: