$fgets()-- different results in different simulators?

K

kumar

Guest
Hello all,

I have one interesting point to make regarding system task $fgets().

testin = $fopen("test.txt", "r");
str_len = $fgets(l, testin);
$display("%s, %h, %d",l, l, str_len);

I have used Model-Sim, Active Hdl and NC-Sim in PC Environment and did
find that CR- Carriage Return is not displayed by Modelsim and Active
HDL simulators when $display is done .
Is there any reason for that?

But NC-Sim displays both CR and LF ie., its equivalent is 0d and 0a
hex value respectively.
Is it a bug in Model-Sim/ActiveHDL.

If NC-Sim is displaying the character CR+LF, Model-sim/Active-HDL just
reports LF.

But i should be able to display the character "CR" x0d.


Well one more point is that if i do $fseek, In Model-Sim/ActiveHDL it
would result in
p = $fseek(testin, -str_len-1, 1); to go back to the start/Begining of
the line.

but in NC-Sim i need to do just this
p = $fseek(testin, -str_len, 1);

Hence i have to use two different statements for differnet simulators.
Looks like Model-Sim and ActiveHdl is not displaying the character
value of "CR"

Can anybody help me out in this regard

Thanks in advance

Regards
Kumar
 
kummikd@yahoo.com (kumar) wrote in message news:<35043f4b.0307310510.3b3f4e7d@posting.google.com>...
I have used Model-Sim, Active Hdl and NC-Sim in PC Environment and did
find that CR- Carriage Return is not displayed by Modelsim and Active
HDL simulators when $display is done .
Is there any reason for that?
I am guessing that you are running on a Windows system rather than
Unix. Windows systems use CR+LF to end text lines, while Unix uses
just LF. To try to make text processing applications portable from
Unix to Windows (or originally DOS), the C fopen() on Windows has
both text and binary modes. In text mode, CR+LF gets converted into
just LF when read, and LF gets converted into CR+LF when written.
That allows typical C code that assumes lines end with LF to work
under Windows. Binary mode does not do this translation, so that
you can read raw data without losing some bytes.

This would account for the CR characters disappearing on you, and
for the $fseek counts not matching the number of characters that
you thought you read. If you want to prevent this, you should be
able to force the files to be opened in binary mode. Just change
the file mode from "r" to "rb" in the $fopen call. It appears
that NC is always opening in binary mode.
 

Welcome to EDABoard.com

Sponsor

Back
Top