When should endfile be used, before or after reading? - vhdl

While looking for examples of reading text from a file (using readline and read), I constantly see code like the following, but haven't found information on what exactly endfile does.
while not endfile(in_file) loop
readline(in_file, in_line);
read(in_line, in_data);
end loop;
In all other languages I know, end of file would be checked after reading the line from the file, not before.
What exactly are the semantics of endfile that make this usage correct?

From the IEEE VHDL Standard:
3.4.1 File operations ... Function ENDFILE returns FALSE
if a subsequent READ operation on an open file object whose access mode is read-only can retrieve another value from the file; otherwise, it returns TRUE.
So checking endfile prior to reading from the file is correct, since endfile returns false as long as another value/line can be read.

There are several languages which use an eof function or statement to determine the end of a file. These languages or library have no special return value (-1, null, nil, ...) or even a exception mechanism to report a failed read(..), so in this case its necessary to test eof before a read(..) and proof that one more read(..) won't crash the program.
Edit 1
zennehoy asked for some language examples, which use this kind of EOF statement/function:
Example 1: QBasic
open "file.txt" for input as #1
do until eof(1)
input #1,name$,street$, ...
loop
close #1
Example 2: Windows Scripting Host (WSH), alias VBA, alias VB Script:
do until fileHandle.atEndOfStream
[...]
loop
Example 3: if I remember correctly this would be Pascal
Ansi-C has also a eof functionality, but in many cases its not necessary to use it, because you can determine the end of a file, if read(..) returns less bytes than the requested bytes.
But this won't function if you are reading from streams/pipes/... because read can return less bytes and after several milliseconds there are more bytes and you can read again. A test for EOF will assure you, that a writer on a stream is closed.

Another example language where the end of file status is tested first is Ada as in while not End_Of_File loop, where End_Of_File is a function in package Ada.Text_IO and the default value for it's single argument of type File_Mode is In_file. As of Ada95, End_Of_File only operates on files of mode In_File or Inout_File otherwise generating an exception. (While endfile will return FALSE for WRITE_MODE in VHDL).
Early implementations of VHDL included preprocessors for Ada in the days of big iron. You can see the influence throughout the LRM. For instance there is no coupling between bit positions and integer values or description of a float format. These happen to allow VHDL to operate on a computer with native decimal ALUs.

Related

What does od mean in mean in pseudo-code?

My lecturer has presented me with the following pseudo-code:
at each timer interrupt
do
• perform analog-to-digital
conversion to get y;
• compute control output u;
• output u and do digital-to-analog conversion;
od
I am wondering what od could mean in this context?
Maybe it is part of the programming language I am not aware of?
'do' and 'od' is a "mirror" text block instruction that start with "do" and read software lines and end instruction with "od", because symbols "{" , "}" not is allowed,not exist, or means other type of instruction. some pseudo-code type with a limited set of instructions like architecture 'RIS' has compilators that read it.
Seems to be the end of the do block. At first I thought it was a loop but it may just be a block that executes once.

What is the colon before Fortran if `something:if(some_condition) then`?

I am working through some code other people have written and found a piece of Fortran syntax that I haven't seen yet and don't exactly understand nor can seem to find anything on the web about (probably because I don't know what it's called).
The code looks like this:
bisection_or_ordering:if(ordering /= 'bisection') then
...
do stuff
...
end if bisection_or_ordering
bisection_or_ordering is not a variable and not declared anywhere in the code.
What does this do? What is it for? And what is it called?
The part before the colon is a construct name.
Executable constructs with blocks - if, block, associate, critical, select case, select type and of course do - may optionally have these construct names.
They are useful both for identification (for clarity with nested or long constructs) but also for control under the exit statement (except to escape critical or do concurrent blocks).
The construct name may appear on the closing statement of the block, as in the question's example, but it is optional and must match if present.

Placing characters after an end statement - any benefit beyond readability?

Beyond possible readability benefits, is there any reason to place a descriptor following an end in Fortran, e.g.:
subroutine foo()
!> small code fragment
end subroutine foo
Versus:
subroutine foo()
!> small code fragment
end subroutine
Obviously, for any code block that can't be viewed in a single instance, I can immediately see the value of such a closing statement, but, especially for small functions and type definitions it feels unnecessary verbose.
Not looking for a "opinion on whether its good style", or not, answer, but trying to understand further if there is something I am misunderstanding about the construction on such statements.
If a name is provided on an end statement, and that name does not match the name in the opening statement of the program unit or subprogram, then a conforming compiler is required to issue a diagnostic. This may help locate coding errors.
Otherwise this is purely a question of style.

What is the purpose of the pipe character in Go's os.OpenFile flag argument?

When using the OpenFile function in Go's os package, what exactly is the purpose of the pipe character?
Example:
os.OpenFile("foo.txt", os.O_RDWR|os.O_APPEND, 0660)
Does it serve as a logical OR? If so, does Go choose the first one that is "truthy"?? Being that the constants those flags represent, at the heart of them are just integers written in hexadecimal, when compiled how does Go choose which flag to apply?
After all, if the function call were to go by the largest number, os.O_APPEND would take precedence over all other flags passed in as seen below:
os.O_RDWR == syscall.O_RDWR == 0x2 == 2
os.O_APPEND == syscall.O_APPEND == 0x400 == 1024
os.O_CREATE == syscall.O_CREAT == 0x40 == 64
UPDATE 1
To follow up on the comment below, if I have a bitwise operator calculation using os.O_APPEND|os.O_CREATE will that error if the file exists, or simply create/append as needed?
UPDATE 2
My question is two fold, one to understand the purpose of the bitwise operator, which I understand now is being used more as a bitmask operation; and two, how to use the os.OpenFile() function as a create or append operation. In my playing around I have found the following combination to work best:
file, _ := os.OpenFile("foo.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
file.WriteString("Hello World\n")
file.Sync()
Is this the correct way or is there a more succinct way to do this?
It is a bitwise, not a logical OR.
If you write out the numbers in binary, and assign each a truth value 0/1, and apply the logical OR to each of the bits in place i between the arguments, and then reassemble the result into an integer by binary expansion - that's the | operator.
It is often used in a way that is commonly described as a "bitmask" - you use a bitmask when you want a single int value to represent a (small) set of switches that could be turned on or off. One bit per switch.
You should see in this context, A | B means "all the switches in A that are on, as well as all the switches in B that are on". In your case, the switches define the exact behavior of the file open/creation function, as described by the Go manual. (And probably more in detail by the Unix manpage I linked above).
In a bitmask, constants are typically defined that represent each switch - that's how those O_* constants are determined. Each is an int with exactly one bit set and represents a particular switch. (though, be careful, because sometimes they represent combinations of switches!).
Also:
^A // All of the "switches" not currently on in A
A&^B // All of the "switches" on in A but not on in B
A^B // All of the "switches" on in exactly one of A or B
, etc.
The operator | itself is described in the Go manual here.
It is a bitwise OR operator. Its purpose being used here is to allow for multiple values to be passed as a bitmask. Thus you can combine flags to create a desired result such as using the OpenFile() function to create a file if it does not exist or append to it if it does.
os.Openfile("foo.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660
The constants being passed as arguments from the os package are assigned values from the syscall package. This package contains low-level operating system independent values.
Package syscall contains an interface to the low-level operating system primitives. The details vary depending on the underlying system, and by default, godoc will display the syscall documentation for the current system. If you want godoc to display syscall documentation for another system, set $GOOS and $GOARCH to the desired system. For example, if you want to view documentation for freebsd/arm on linux/amd64, set $GOOS to freebsd and $GOARCH to arm. The primary use of syscall is inside other packages that provide a more portable interface to the system, such as "os", "time" and "net".
https://golang.org/pkg/syscall/
As noted by #BadZen, a bitwise OR operator, in this case the '|' character, acts at the binary level changing any 0 values to 1's that are not already ones.
You should see in this context, A | B means "all the switches in A that are on, as well as all the switches in B that are on".
By doing this as the function above displays, you are manipulating the behavior of the function to create a file (os.O_CREATE) with the given name of foo.txtor open the file for reading/writing (os.O_RDWR) and any value written to it will be appended (os.O_APPEND). Alternatively you could pass along os.O_TRUNC in order to truncate the file before writing.
The bitwise OR operator allows you a powerful solution to combining different behaviors in order to get the result from the function that you are desiring.

Julia writing to binary error

I'm trying to write to binary data using from a partitioned data frame. Generally, this process works fine however occasionally I get some errors. I have a written a basic conditional to address the error (I have also used try/catch blocks but I'm working with a relatively large data set and so I think the Boolean might be faster if that assumption is false feel free to make fun of me and/or my friends). Here is some code:
for x in RICT["$i"]["Numbers"]
if typeof(x) == "NAtype"
write(f3, convert(ASCIIString, "$x" ))
else
write(f3, convert(Int32, x ) )
end
end
here is the error which my diminutive understanding of life and Julia tells me I shouldn't be seeing:
no method convert(Type{Int32},NAtype)
Thanks so much.
The output of typeof(x) is not a string so it will never match "NAtype". Remove the quotation marks from around NAtype and then it should work.

Resources