Forum > Beginners

ReadFileToString 'raises an exception' - but it doesn't?

<< < (2/3) > >>

Thaddy:
ROFL.. :D

dsiders:

--- Quote from: AlexTP on July 17, 2024, 09:32:53 am ---Docs:
>If there is an error while reading the file, an Exception is raised and an empty string is returned.

Docs are correct. During READING the exception is raised.
If file not found - no exception occurs <-- this is omitted in docs.
Docs should be improved.

--- End quote ---

I've written an update to the topic. It looks like this:


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ReadFileToString Returns a string with the contents of the named file. ReadFileToString opens the file specified in the FileName argument, then reads its contents into the Result string. FileName can include a path to the file, but cannot include a value which needs to be resolved (like '~' for the home directory on UNIX-like file systems). Relative path notation can be used. ReadFileToString does not check whether FileName contains a fully-expanded value or whether it exists on the local file system. It does not complain if the file does not exist; the return value is an empty string ('') for these conditions. ExpandFileName() or FileExists() should be called for the file path/ name before ReadFileToString is called. FileSize is used to determine the size needed for the return value. UNIX-like file systems usually return 0 as the file size if FileName is located on a virtual file system (like /proc). In this case, the return value is an empty string. Another mechanism (like TStringList.LoadfromFile) should be used for virtual files. FileOpenUTF8 is called to get a shared handle used to read from (but not write to) the specified file name. If an invalid handle is returned, the routine quietly exits with an empty return value. FileRead is called for the handle to read the number of bytes in the file size into the return value. FileClose is called to close the handle for the file when the operation has been completed. Errors If an error occurs while reading from or closing the file handle, the Exception is handled in the routine and an empty string is returned.  
Feedback appreciated.

wp:
Line 3: To distinguish from other files I would prefer to see "text file" in this line rather than "file" alone.

Usage of absolute or relative paths is not clear (did not test it myself, though): In line 10 you say "Relative path notation can be used", but in line 10 you require ExpandfileName to be called which would indicate that relative paths are not allowed.

dsiders:

--- Quote from: wp on July 17, 2024, 07:35:14 pm ---Line 3: To distinguish from other files I would prefer to see "text file" in this line rather than "file" alone.

--- End quote ---

Thanks Werner. Changed .


--- Quote from: wp on July 17, 2024, 07:35:14 pm ---Usage of absolute or relative paths is not clear (did not test it myself, though): In line 10 you say "Relative path notation can be used", but in line 10 you require ExpandfileName to be called which would indicate that relative paths are not allowed.

--- End quote ---

Relative paths work when valid. Otherwise, it's an empty string.
ExpandFileName was mentioned as a remedy for '~' notation.
I have changed that paragraph to:


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ReadFileToString does not check whether FileName contains a n expanded value or whether it exists on the local file system. It does not complain if the file does not exist; the return value is an empty string ('') for these conditions. ExpandFileName() can be used to resolve a path with '~' notation. FileExists() or FileExistsUTF8() can be used to verify whether the file path/name exists before ReadFileToString is called. 
Does that help?

Thaddy:
Why not simply warn that you have to check for existance before you call the function?
I like Tron's unreachable code example to demonstrate that. Or did I miss something...
A beautiful joke for programmers. :) :) :)

But the suggested adaptation by Werner is otherwise perfectly worded.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version