2 single: Files; Introduction
8 In this chapter we are going to learn about files functions.
41 pair: Files; Read File using Read()
46 We can read the file content using the Read() function
52 Read(cFileName) ---> String contains the file content
58 see read("myfile.txt")
60 The read function can read binary files too
70 pair: Files; Write file using Write()
75 We can write string to file using the Write() function
77 The write function can write binary data to binary files.
83 Write(cFileName,cString) # write string cString to file cFileName
90 cFile = read("ring.exe")
91 write("ring2.exe",cFile)
99 We can get the folder contents (files & sub folders) using the Dir() function.
105 Dir(cFolderPath) ---> List contains files & sub folders.
107 This function returns a list and each list item is a list of two items
109 * File/sub folder name
111 * Type (0 = File , 1 = Folder/Directory)
117 see "Testing DIR() " + nl
118 mylist = dir("C:\myfolder")
121 see "Directory : " + x[1] + nl
123 see "File : " + x[1] + nl
126 see "Files count : " + len(mylist)
129 pair: Files; Rename()
134 We can rename files using the Rename() function
140 Rename(cOldFileName,cNewFileName) ---> Number ( Status: Success (0) , Error (-1) )
146 rename("file.txt","help.txt")
149 pair: Files; Remove()
154 We can delete a file using the Remove() function
175 We can open a file using the Fopen() function
181 Fopen(cFileName,cMode) ---> File Handle
183 ===== ==========================================
185 ===== ==========================================
186 "r" Reading (The file must exist)
187 "w" Writing (create empty file / overwrite)
188 "a" Appends (create file if it doesn't exist)
189 "r+" update (reading/writing)
190 "w+" Create empty file (reading/writing)
191 "a+" reading & appending
192 ===== ==========================================
195 pair: Files; Fclose()
200 When we open a file using fopen() function, we can close it
201 using the Fclose() function
210 pair: Files; Fflush()
215 We can flushes the output buffer of a stream using the Fflush() function
224 pair: Files; Freopen()
229 We can open another file using the same file handle and at the same time close
236 Freopen(cFileName,cMode,file handle) ---> file handle
242 freopen("myprogoutput.txt","w+",stdout)
249 ** Read : https://en.wikipedia.org/wiki/Device_file#Device_files
250 ** The next code is not portable, we can use iswindows() before
251 ** using it and we can write special code for each operating system.
254 freopen("CON","w",stdout) # For Microsoft Windows
255 see "Done" + nl # print to stdout again
264 # Output to file : myprogoutput.txt
278 pair: Files; Tempfile()
283 The function Tempfile() creates a temp. file (binary).
285 The file will be deleted automatically when the stream is closed
291 TempFile() ---> file handle
294 pair: Files; Tempname()
299 We can generate temp. file name using the Tempname() function
301 The generated name will be different from the name of any existing file
307 Tempname() ---> generated file name as string
315 We can set the file position of the stream using the Fseek() function
321 Fseek(file handle, nOffset, nWhence) ---> zero if successful
323 The next table presents the nWhence values
325 ===== ===============================
327 ===== ===============================
331 ===== ===============================
339 We can know the current file position of a stream using the Ftell() function
345 Ftell(file handle) ---> file position as number
348 pair: Files; Rewind()
353 We can set the file position to the beginning of the file using the Rewind() function
362 pair: Files; Fgetpos()
367 We can get handle to the current file position using the Fgetpos() function
373 Fgetpos(file handle) ---> position handle
376 pair: Files; Fsetpos()
381 We can set the current file position using the Fsetpos() function
387 Fsetpos(file handle,position handle)
390 pair: Files; Clearerr()
395 We can clear the EOF error and the error indicators of a stream using the clearerr()
402 Clearerr(file handle)
410 We can test the end-of-file indicator using the Feof() function
416 Feof(file handle) ---> returns 1 if EOF and 0 if not
419 pair: Files; Ferror()
424 We can test the error indicator of a given stream using the Ferror() function
430 Ferror(file handle) ---> returns 1 if error and 0 if not
433 pair: Files; Perror()
438 We can print error message to the stderr using the Perror() function
444 Perror(cErrorMessage)
452 We can get the next character from the stream using the Fgetc() function
458 Fgetc(file handle) ---> returns character or EOF
467 We can read new line from the stream using the Fgets() function
473 Fgets(file handle,nSize) ---> string
475 The function stop when nSize characters are read, new line character is read or EOF.
483 We can write a character to the stream using the Fputc() function
489 Fputc(file handle,cChar)
497 We can write a string to the stream using the Fputs() function
503 Fputs(file handle,cString)
507 pair: Files; Ungetc()
512 We can push a character to the stream using the Ungetc() function
514 The character will be available for the next read
520 Ungetc(file handle,character)
529 We can read data from a stream using the Fread() function
535 Fread(file handle,nSize)
538 pair: Files; Fwrite()
543 We can write data to a stream using the Fwrite() function
549 Fwrite(file handle,cString)
553 pair: Files; Fexists()
558 We can check if a file exists using the Fexists() function
564 Fexists(cFileName) ---> returns 1 if the file exists
570 see fexists("b:\mahmoud\apps\ring\ring.exe") + nl +
571 fexists("b:\mahmoud\apps\ring\ring2.exe") + nl
586 The next program test some of the file functions
590 See "testing file functions" + nl
593 fp = fopen(exefolder() + "../tests/scripts/s65.ring","r")
596 fp = freopen(exefolder() + "../tests/scripts/s78.ring","r",fp)
597 See "close file" + nl
607 remove(exefolder() + "../tests/scripts/mytest2.txt")
608 write(exefolder() + "../tests/scripts/tests1.txt","hello")
609 rename(exefolder() + "../tests/scripts/test1.txt",exefolder() +
610 "../tests/scripts/mytests2.txt")
612 see "print file" + nl
613 fp = fopen(exefolder() + "../samples/fromdoc/filefuncs.ring","r")
621 see nl+"print line from the file" + nl
622 fp = fopen(exefolder() + "../samples/fromdoc/filefuncs.ring","r")
626 fp = fopen(exefolder() + "../tests/scripts/test78.txt","w+")
627 fseek(fp,0,2) # goto end of file
635 see "print file" + nl
636 see read(exefolder() + "../tests/scripts/test78.txt")
638 fp = fopen(exefolder() + "../tests/scripts/test78.txt","r")
639 see "testing ungetc() " + nl
647 see "testing fread() " + nl
648 fp = fopen(exefilename(),"rb")
653 see "testing fwrite() " + nl
654 fp = fopen(exefolder() + "../tests/scripts/test1.txt","wb")
658 The next example print part of the content of a binary file
662 see "Testing: fread()" +" FileName: "+ exefilename() +nl +nl
663 fp = fopen(exefilename(),"rb")
666 if isprint(substr(r, n, 1))
679 pair: Files; Numbers and Bytes
684 The next functions to convert between Numbers and Bytes.
697 see "Test Int2Bytes() and Bytes2Int() - Value : 77" + nl
699 see "Int Size : " + len(r) + nl
701 see Bytes2Int(r) + nl
702 see "Test Float2Bytes() and Bytes2Float() - Value 77.12" + nl
703 r = Float2Bytes(77.12)
704 see "Float Size : " + len(r) + nl
706 see Bytes2Float(r) + nl
707 see "Test Double2Bytes() and Bytes2Double() - Value 9999977.12345" + nl
708 r = Double2Bytes(9999977.12345)
709 see "Double Size : " + len(r) + nl
712 see Bytes2Double(r) + nl