Fortran can't read from file - fortran

I am trying to read multiple variables from a txt file I created using Fortran. The number of lines of the file was random as well as the numbers written on each line.
The file looks something like this:
1061 2.5 5.0 7.5 3.5
1062 9.0 2.5 10.0 7.5
Then I open the file on a separate Fortran program and try to read from it.
My code looked something like this, a is an integer, while b, c, d, e and f are all real values:
open(10,file='data.txt',form='unformatted')
do
read(10,*,iostat=st) a,b,c,d,e
if(st==-1) exit
f=a+b+c+d+e
end do
When I try to run the program, than a runtime error appears telling me that I am referring on undefined variables and when I try to run the debugger, the variables a, b, c, d and e stay undefined even after the read command.

It seems to me that your file is a formatted file (in fact you use * as format). However you define it as 'unformatted' in the open statement. Try to set
form='formatted'
in your open statement or just omit the form= clause, since the default is formatted.

Just summarizing the comments and the existing answer, you should remove the
'unformatted' keyword in the open statement, as fortran reads text files
as your data.txt as formatted one by default.
Assuming that your text file might look like this:
1061 2.5 5.0 7.5 3.5
1062 9.0 2.5 10.0 7.5
1063 4.0 3.1 3.2 5
1064 2.1 1.9 ***** 7.8
1065 1.0 4.0 10.0 3.5
1066 4.4 1.9 2.5
1067 6.7 8.8 10.9 12.0
then you should handle the different formatting errors following this
minimal example:
program FileIO
implicit none
character(256) :: line
character(80) :: msg
integer :: a,st
real :: b,c,d,e,f
open(10,file='data.txt')
do
write(*,'(A)') '------------------------------------------------------------'
read(10,'(A)',iostat=st) line ! Buffer input in line
write(*,'(A)') 'Reading of line '//trim(line)
if (st < 0) then ! End of file or end of record
exit
else
read(line,*,iostat=st,iomsg=msg) a,b,c,d,e
write(*,'(A)') 'IO-message is: '//trim(msg)
if (st == 0) then ! Read one line successfully
write(*,'(A)') 'Line successfully read: '//trim(line)
f=a+b+c+d+e ! Calculate result
else
write(*,'(A)') 'IO-error occured in line: '//trim(line)
f=0
endif
endif
end do
close(10)
end program FileIO
A negative result for iostat indicates an end of file or end of record event. A positive result for iostat indicates a run-time error message, see e.g. for Intel Fortran.
This should be handled by an if condition.
I recommend you to buffer the file input in a character variable, e.g. line.
It helps you to write the error generating line back to a log file or standard
output.
The minimal example generates this output:
------------------------------------------------------------
Reading of line 1061 2.5 5.0 7.5 3.5
IO-message is:
Line successfully read: 1061 2.5 5.0 7.5 3.5
------------------------------------------------------------
Reading of line 1062 9.0 2.5 10.0 7.5
IO-message is:
Line successfully read: 1062 9.0 2.5 10.0 7.5
------------------------------------------------------------
Reading of line 1063 4.0 3.1 3.2 5
IO-message is:
Line successfully read: 1063 4.0 3.1 3.2 5
------------------------------------------------------------
Reading of line 1064 2.1 1.9 ***** 7.8
IO-message is: list-directed I/O syntax error, unit -5, file Internal List-Directed Read
IO-error occured in line: 1064 2.1 1.9 ***** 7.8
------------------------------------------------------------
Reading of line 1065 1.0 4.0 10.0 3.5
IO-message is: list-directed I/O syntax error, unit -5, file Internal List-Directed Read
Line successfully read: 1065 1.0 4.0 10.0 3.5
------------------------------------------------------------
Reading of line 1066 4.4 1.9 2.5
IO-message is: end-of-file during read, unit -5, file Internal List-Directed Read
IO-error occured in line: 1066 4.4 1.9 2.5
------------------------------------------------------------
Reading of line 1067 6.7 8.8 10.9 12.0
IO-message is: end-of-file during read, unit -5, file Internal List-Directed Read
Line successfully read: 1067 6.7 8.8 10.9 12.0
------------------------------------------------------------
Reading of line 1067 6.7 8.8 10.9 12.0
The list-directed reading of line 1063 works fine, even if the number 5 is
given as integer to the real variable e. The formatting error ***** of line
1064 is detected correctly as well as the missing number in line 1066.
Please have a look to the Intel Fortran help regarding list-directed reading,
if you need more information.
Hope it helps.

Related

Stuck in Stallman's GDB book trying debug m4 (macro processor) 'bug' example: m4 executable i have is a direct bin in /bin (nothing ".../gnu/ ./m4")

(There is another post here in Stackoverflow: (Which version of m4 does the author of Debugging with GDB use?), but answer referred links are broken and the solution doesn't look much deep or to the point. I have to say i tried them and also tried to look in other m4 versions in the gnu repository, but even that: "len_lquote = strlen(lquote);" looks deprecated since 2006 version, oldest i found).
VERY EASY EXPLANATION: first example in Stallman GDB book refers to a "./m4" executable (first weird sensation with that "./"), allegedly to be present in some (some old standard installation, perhaps?):
/gnu/m4 (?)
or
/work/Editorial/gdb/gnu/m4/ (?)
(and as i pointed, it looks to be executed with './' like it was indeed not like an environment executable [like mine. My "m4" that i installed through "sudo apt install m4" for the purpose]).
The problem is that if i run "gdb m4", it doesn't do the anything similar with the book's m4:
SHOULD BE (for example, setting a breakpoint in a KNOWN function. KNOWN, because i guess it should be some .c or something like that that GDB should load/consult in parallel with the executable, no?):
(gdb) break m4_changequote
Breakpoint 1 at 0x62f4: file builtin.c, line 879.
MINE:
$ gdb m4
GNU gdb (Debian 12.1-4+b1) 12.1 Copyright (C)
2022 Free Software Foundation, Inc. (......) Reading symbols from
m4... (No debugging symbols found in m4)
(gdb) break m4_changequote
Function "m4_changequote" not defined. Make breakpoint pending on
future shared library load? (y or [n]) n
Any USEFUL (direct to the point) help?
Any SOLUTION?
Any EQUIVALENT path?
VERY EASY EXPLANATION: first example in Stallman GDB book refers to a "./m4" executable (first weird sensation with that "./"), allegedly to be present in some (some old standard installation, perhaps?)
You are coming to this problem with the wrong set of assumptions.
In general, you most often use GDB on your own programs, i.e. programs you coded yourself. You usually debug these programs in the directory in which you built them, which is the current directory. On UNIX, current directory is named ., and if you want to refer to a program in your current directory, you use ./program notation.
In the book, Stallman is debugging m4 which he worked on and built.
You are trying to debug m4 which you didn't build, and thus you are having problems following the book.
I suggest that you actually download m4 sources and build it yourself (it should be as simple as ./configure && make), and then follow the book.
Debugging the system-provided m4 is also possible, but that's not what the book is about.
Update:
I did my homework and went through many files (.c, .h, etc) in many folders of m4 versions and i couldn't find anything like what you pointed back in your 2009 post: "...You can download any version of m4, change len_lquote = strlen(lquote); to len_lquote = strlen(rquote); in set_quotes(), and then redo the sample debugging session. ...".
The 2009 post referenced above is apparently this one.
I downloaded m4 version using:
git clone git://git.sv.gnu.org/m4
cd m4
git checkout branch-1.4
In src/input.c, I see this code:
719 void
720 set_quotes (const char *lq, const char *rq)
721 {
722 free (lquote.string);
723 free (rquote.string);
724
725 /* POSIX states that with 0 arguments, the default quotes are used.
726 POSIX XCU ERN 112 states that behavior is implementation-defined
727 if there was only one argument, or if there is an empty string in
728 either position when there are two arguments. We allow an empty
729 left quote to disable quoting, but a non-empty left quote will
730 always create a non-empty right quote. See the texinfo for what
731 some other implementations do. */
732 if (!lq)
733 {
734 lq = DEF_LQUOTE;
735 rq = DEF_RQUOTE;
736 }
737 else if (!rq || (*lq && !*rq))
738 rq = DEF_RQUOTE;
739
740 lquote.string = xstrdup (lq);
741 lquote.length = strlen (lquote.string);
742 rquote.string = xstrdup (rq);
743 rquote.length = strlen (rquote.string);
744 }
Obviously there is no longer len_lquote = strlen(lquote);, but the equivalent statement is now lquote.length = strlen (lquote.string); on line 741.
To introduce a bug, you would change line 741 to read lquote.length = strlen (rquote.string);
But suppose you really want the source to match what was described in the book. The book was first published in 1988, and the very first version of input.c in the Git repo is from 2000, so we need to find older version of m4 sources.
I found a reference to m4-1.0.3.tar.Z from 1992 here, and the file itself here: http://www.nic.funet.fi/index/gnu/funet/historical-funet-gnu-area-from-early-1990s/m4-1.0.3.tar.Z
In that TAR file, m4-1.0.3/input.c does have the source you are looking for:
555 void
556 set_quotes (char *lq, char *rq)
557 {
558 if (lquote != def_lquote)
559 xfree (lquote);
560 if (rquote != def_rquote)
561 xfree (rquote);
562
563 lquote = (lq == NULL) ? def_lquote : xstrdup (lq);
564 rquote = (rq == NULL) ? def_rquote : xstrdup (rq);
565
566 len_lquote = strlen (lquote);
567 len_rquote = strlen (rquote);
568 }
Beware: that source is very old and does not build with modern compilers (GCC-12.2.0 in my case). You'll need to get an ancient version of GCC to build it.

Generate PDF with C++ and Latex

Would it be possible to generate PDF from c++ source code using latex ?
I´m currently using html, QWebEngine and QPrinter to create PDF.
But there is some issues like pages jump. Latex will be a good solution to ensure some graphics element are well rendered.
Working with Windows only. Crossplatform solution is not needed
Here are the steps I did to setup pythontex on my windows 10 system.
Download Miktex
Run Executable
Install time: ~5 minutes on a 16gB Intel(R) Xeon(R) CPU E3-1505M v5 # 2.80GHz, 2801 Mhz, 4 Core(s), 8 Logical Processor(s)
Miktex base size ~10mB at **/appdata/local/miktex/*. Note, this may not be where al the files are located. IDK
Test if pdf latex is installed. Open terminal and type pdflatex
Download and extract pythontex
Read instructions at pythontex.pdf.
Install python tex using pythontex_install.bat
Add pythontex to path.
Run a pythontex example
\documentclass[11pt]{article}%
\usepackage{pythontex}
\usepackage{nopageno}
\begin{document}
\begin{pyconsole}
x = 987.27
x = x**2
\end{pyconsole}
The variable is $x=\pycon{x}$
\end{document}
In order to compile do
pdflatex my-latex.tex
pythontex my-latex.tex
pdflatex my-latex.tex
May need to install additional package for it to compile. My ending size in apdata/local grew alot.... 814 MB

Why is libFuzzer on Windows yielding error: "no interesting inputs were found"?

About half a year ago I had setup a CMake project with VSCode with a libFuzzer target that ran on Windows and macOS. I use the C++ extension along with the CMakeTools extension from Microsoft.
When I resumed the project again now I'm getting an error at the end of the run of the fuzzer:
ERROR: no interesting inputs were found. Is the code instrumented for coverage? Exiting.
Full output:
INFO: Seed: 2201882200
INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes
INFO: A corpus is not provided, starting from an empty corpus
#2 INITED exec/s: 0 rss: 61Mb
ERROR: no interesting inputs were found. Is the code instrumented for coverage? Exiting.
Compared to the same fuzzer when it's run on macOS:
INFO: Seed: 1824512455
INFO: Loaded 1 modules (7 inline 8-bit counters): 7 [0x10c2baa88, 0x10c2baa8f),
INFO: Loaded 1 PC tables (7 PCs): 7 [0x10c2baa90,0x10c2bab00),
INFO: 6 files found in /Users/thomas/SourceTree/vscode-cmake-libfuzzer/fuzzers/corpus/fuzz_test/
INFO: seed corpus: files: 6 min: 1b max: 10b total: 37b rss: 30Mb
#7 INITED cov: 1 ft: 1 corp: 1/1b exec/s: 0 rss: 30Mb
#1048576 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 524288 rss: 1099Mb
#2097152 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 699050 rss: 1100Mb
#4194304 pulse cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 599186 rss: 1101Mb
#5740032 DONE cov: 1 ft: 1 corp: 1/1b lim: 8192 exec/s: 521821 rss: 1101Mb
Done 5740032 runs in 11 second(s)
stat::number_of_executed_units: 5740032
stat::average_exec_per_sec: 521821
stat::new_units_added: 0
stat::slowest_unit_time_sec: 0
stat::peak_rss_mb: 1101
Snippets of how I linked libFuzzer: (I added the libs to Windows manually as one must to make it link.)
# https://llvm.org/docs/LibFuzzer.html#fuzzer-usage
target_link_libraries(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
target_compile_options(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
The test fuzzer:
#include <iostream>
#include <stddef.h>
#include <stdint.h>
bool FuzzMe(const uint8_t *Data, size_t DataSize) {
return DataSize >= 3 &&
Data[0] == 'F' &&
Data[1] == 'U' &&
Data[2] == 'Z' &&
Data[3] == 'Z'; // :‑<
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
// std::cout << "Hello Fuzzy...\n";
FuzzMe(data, size);
return 0;
}
Minimal complete example:
https://github.com/thomthom/vscode-cmake-libfuzzer
On Windows I had installed clang via the snapshots made available: https://llvm.org/builds/
On macOS I installed it via brew install llvm (Since AppleClang doesn't include libFuzzer)
I've tested this on two Windows machines now that both used to run the fuzzer fine, now they don't. But I've not able to figure out what caused the regression. I know I didn't update Clang on either systems. On one I also tried installing Clang via Visual Studio, and I tried selecting that installation as well from VSCode to see if it made any difference, but no avail.
Only other thing I can think of is that my VSCode CMake projects needed adjustments after the VSCode-Tools extension updated the default generator for Windows. I don't recall what it used to use. I still find it strange if that should affect the libFuzzer.
It turned out to be caused by my CMake config omitting these lines for Windows builds:
target_link_libraries(clang_fuzzer INTERFACE
-fsanitize=fuzzer,address
)
I think some cache came into play here that caused me not to detect this when I did the initial work on this.
Commit for fix in my above example:
https://github.com/thomthom/vscode-cmake-libfuzzer/commit/d7553fe01f88c71c19ff8f833b4235b8a3d17f99
Hoping that it can be useful for some people, I am posting my experience with this error.
In my case, I was getting this error merely because I changed the permission of the corpus folder (see the command below). The reason I was doing it because libfuzzer was not adding new inputs to the corpus folder.
chmod 644 /corpus (this command caused the error)
To avoid this error, I tried another method to be able to add new inputs to the folder; changed the owner of corpus folder to the user of the fuzzer process (i.e., daemon in my case).
mkdir /corpus;chown daemon:daemon /corpus (this command helped)

POSIX regex for Version string

I have a source on which I have no control and I want to filter out all string which have some characters in it.
For example Out of these:
9
8.1.0
5.0
9.0
5.1
8.0.0
7.0 (cdfsdsdsd)
5.0.2
8
7.0.1
7.1
6.0
7.0
Over 32323
7.0 rdx K9 bnsm
9.2.3
8.oo
pp
unknown
8.0_vgfe10051988
6.0.1
8.0.0-derv10051988
9.1
9.0.0
8.0.1
7.0_xccv10051988
7.1.3
10.0
7.0.X.1.C
8.0.0_vged10051988
4.4.4
7.1.2
7.0 [NKL 24 | ABC]
8.1
7.1.1
5.1.1
7.0_Jgrd10051988
9.XXX
9.0.1
8.0
5.0.1
8.1.1
10
Out of these I need only those Strings with only digits and .
9
8.1.0
5.0
9.0
5.1
8.0.0
5.0.2
8
7.0.1
7.1
6.0
7.0
9.2.3
6.0.1
9.1
9.0.0
8.0.1
7.1.3
10.0
4.4.4
7.1.2
8.1
7.1.1
5.1.1
9.0.1
8.0
5.0.1
8.1.1
10
I have tried many regex, but nothing seems to be generic enough,
This regex is giving [0-9]*.?[0-9] Strings too.
The one I have got working is ^(\*|\d+(\.\d+){0,2}(\.\*)?)$, but this is not POSIX.
How do I get a POSIX which also works on Redshift?
By taking a look of the Amazon document, it seems POSIX ERE is supported by the Redshift. Then would you please try:
^[[:digit:]]+(\.[[:digit:]]+)*$
Your regex works, you just need to use double backslashes in the string literal.
According to the Amazon Redshift "POSIX Operators" documentation,
Amazon Redshift supports the following Perl-influenced operators in regular expressions. Escape the operator using two backslashes (‘\\’).
So, you may use
'^(\\*|\\d+(\\.\\d+){0,2}(\\.\\*)?)$'
The simplest is:
^[.0-9]+$
If you don't have support for extended regex, you can do:
^[.0-9][.0-9]*$
I ran this command on your input and output and got an empty diff:
$ diff <(grep -P '^[.0-9]+$' input) output
$ echo $?
0
On your specific input, even ^[.0-9]*$ would work.
Note, however, that there's a difference between "Strings with only digits and ." and "version string". The simple regex will also catch inputs like:
1..2
..
.
0...
.1
If that's not a problem, you can use the simple regex.

GDB permission issue, trying to debug called function

I am writing a cache sytem in fuse (HW assigment) and debugging it with gdb.
im having a problem instructing gdb to add a break point in one of my function that is not part of my executable file.
im using gdb with the following script, where CachingFileSystem is where the main function is, and Cache.o is the file where the function I would like to debug:
exec CachingFileSystem
file Cache.o
break Cache::readFromBlock(Block *, off_t, size_t,char *)
run /tmp/r /tmp/m 10 0.4 0.4
gdb adds the break point correctly -
Breakpoint 1 at 0x267b: file Cache.cpp, line 377.
but when running I get:
(gdb) run /tmp/r /tmp/m 10 0.4 0.4
'ex4/Cache.o' has changed; re-reading symbols.
Starting program: ex4/Cache.o /tmp/r /tmp/m 10 0.4 0.4
ex4/Cache.o: Permission denied.
During startup program exited with code 1.
Debuuging the main program, CachingFileSystem, works as expected without any problems.