Only reading in one input from a file in ada - file-io

I am having some trouble reading input in from a file. So what I have done is made a proof of concept program, which is a piece of my main program that does much more but I am only having trouble reading the input.
Here is my proof of concept program:
WITH Ada.Text_IO; USE Ada.Text_IO;
with ada.Integer_Text_IO; use ada.Integer_Text_IO;
PROCEDURE Open_File IS
subtype idnum is string(1 ..7);
-- Make short names so that we can show where things come from
My_File : File_Type; -- Name for file in this program
Os_Name : String := "My_Data.txt"; -- OS name for the file
N : idnum; -- Temporary for reading and printing file contents
EOL : boolean;
C : character;
BEGIN
-- Open will raise an ADA.IO_EXCEPTIONS.NAME_ERROR expection
-- if the file does not exist.
Open (File => My_File, Mode => In_File, Name => Os_Name);
LOOP
EXIT WHEN End_Of_File (My_File);
Look_Ahead(My_File, C, EOL);
IF EOL THEN
Skip_Line;
ELSE
IF C = ' ' THEN
Get(My_File, C);
ELSE
Get (My_File, N);
Put_Line(N);
END IF;
END IF;
END LOOP;
Close (My_File);
END open_file;
My data file looks like this: (including the spaces with no new lines after the last id)
1234567
456784a
6758abc
When I compile and run my program only the first id number gets printed to the screen. I have no clue where to check my code because it should continue to get id numbers until the end of the file.
Any help would be greatly appreciated. Thanks!

When you Get the second (and third, for that matter) line, Data_Error exception will be raised, because 456784a is not a number, 'a' is not a numeric character. If you want it to be a hexadecimal number, the input should be 16#456784a# (by default).

Related

Trace32 continuously read file

I'm trying to make a script for Trace32 PRACTICE language that reads some data from a .txt file and then passes the values to a variable that I need to debug.
My problem is that I cannot make it return to the top of the file and start over, once the .txt reaches the end. Any ideas ?
Here is what I have at the moment and it enters the IF from the start :
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
PRIVATE &value // declare macro
WHILE TRUE()
(
if FILE.EOFLASTREAD()
(
CLOSE #1
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
)
else
(
READ #1 %LINE &line
&value=STRing.TRIM(STRing.SPLIT("&line",",",0))
PRINT "&value"
V canConfigInitFlag = &value
)
)
CLOSE #1
ENDDO
Thanks !
Closing and re-opening the file to return to the top of the file is the correct approach. (There is no seek-command.)
However there is a bug in your script:
The function FILE.EOFLASTREAD() returns TRUE() if the last READ command did only return empty data because you've reached the end of the file. In your code you do no longer execute the READ command once you've reached the end of the file. Thus FILE.EOFLASTREAD() will continue to return TRUE().
The following will work:
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
PRIVATE &value &line // declare macros
WHILE TRUE()
(
READ #1 %LINE &line
IF FILE.EOFLASTREAD() // Last READ was after the end of the file
(
CLOSE #1
OPEN #1 C:/Sandboxes/JLR_ADAS_DC/Trace32Scripts/ListCam_new.txt /Read
READ #1 %LINE &line // Read 1st line
)
&value=STRing.TRIM(STRing.SPLIT("&line",",",0))
PRINT "&value"
Var.set canConfigInitFlag=&value
)
CLOSE #1
ENDDO

Fortran runtime error: End of file when reading input data

I'm currently running a code and I'm always getting to the same end. I am trying to read an input file and it returns the error:
Fortran runtime error: End of file
In an other post they said to put in the iostat specifier so now my code looks like this:
INTEGER :: m
INTEGER :: st
Open(Unit = 13,action='read',file='Data_Inp.dat',status='old')
read (13,*, iostat = st) m
write (*,*) st
write (*,*) m
ALLOCATE(winkel(m),energie(m))
Do i = 1,m
read(13,*),winkel(i),energie(i)
End Do
And the input file looks like this:
12
-17.83 -0.019386527878
-15.83 -0.020125057233
-12.83 -0.020653853148
-11.83 -0.020840036028
-9.83 -0.020974157405
-8.83 -0.021056401707
-6.83 -0.021065517811
-5.83 -0.020992571816
-4.83 -0.020867828448
-1.83 -0.02069158012
Now the terminal prints a -1 for iostat and a constantly changing number for m.
If the first read command is causing an error, check for extraneous characters before or after "12" in your input file, especially if you created it on one platform (Windows?) and using it on another platform (Linux? Mac?)

Throwing error while printing record element string?

Throwing error while printing record element string using put why please help me to understand?
with ada.text_io;
use ada.text_io;
procedure main is
type my_rec is record
name:string(1..5)of integer;
end record;
var:my_rec;
begin
var.name:="hello";
put(var.name); -- why error?
end main;
error message is below
cc -c hello.adb
hello.adb:7:27: missing ";"
gnatmake: "main.adb" compilation error
hello.adb:7:27 is the coordinates of the error, so take a look in "hello.adb" on line 7 at character position 27.

How do we print characters line by line and save it to csv or text file in PLSQL

DECLARE
V_NUMBER NUMBER :=23;
BEGIN
LOOP
V_NUMBER:=V_NUMBER+1;
EXIT WHEN V_NUMBER:=25;
--Some kind of function to be applied for printing and nesting lines into CSV or TEXT file.
END LOOP;
COMMIT;
END;
Scripting an Oracle SQL Query for Creating a CSV or Text Typed File Output
Consider running this from a SQL Plus session and use the SPOOL command. All output of the SQL command that follows will be written to the file name you specify.
If you need to append your results each successive time the SQL commands are run, then an OS level command would work appropriately when invoking this sqlplus executable block of PL/SQL:
Where the file name of this script is: "sample_csv_out.sql"
DECLARE
v_total_columns constant number:= 3; -- Number of columns queried
v_column_counter number;
v_csv_record varchar2(1000);
c_csv_column_format constant varchar2(15):=
'<<COLUMN1_VAL>>,<<COLUMN2_VAL>>,<<COLUMN3_VAL>>';
cursor result_cur is
SELECT column1, column2, column3
FROM tablea
WHERE column1 = ... ;
BEGIN
v_csv_record:= 'COLUMN1,COLUMN2,COLUMN3';
dbms_output.put_line (v_csv_record);
FOR i in result_cur LOOP
v_csv_record:= replace(c_csv_column_format, '<<COLUMN1_VAL>>', i.column1);
v_csv_record:= replace(v_csv_record, '<<COLUMN2_VAL>>', i.column2);
v_csv_record:= replace(c_csv_record, '<<COLUMN3_VAL>>', i.column3);
dbms_output.put_line(v_csv_record);
END LOOP;
END;
So, for example in a WINDOWS O/S environment, the call to append the output to a specific file name would be:
C:\> sqlplus sample_csv_out.sql >> mycsv_out.csv
The >> notation instructs the operating system to pipe the output of running sample_csv_out.sql via a sqlplus session.
The command DBMS_OUTPUT does the rest. If you need more details, see more Oracle documentation on DBMS_OUTPUT.
COMMENTS: I chose the RECORD STRING TEMPLATE approach to make this script a little more flexible and reusable. I recommend to keep any data manipulation logic within the CURSOR statement. Often when the two are mixed, it gets harder to debug any typos in syntax within a long string of values.
The construction of an output record was also designed to reduce typos, mistakes and frustration... if there are more than 3 columns in your own scripts, adding another element to the output string is mostly a cut-and-paste operation. Likewise with the "header" row (column titles).
You can read and write files in PL/SQL using the UTIL_FILE package
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm

File Output using Gforth

As a first project I have been writing a short program to render the Mandelbrot fractal. I have got to the point of trying to output my results to a file ( e.g. .bmp or .ppm ) and got stuck.
I have not really found any examples of exactly what I am trying to do, but I have found two examples of code to copy from one file to another.
The examples in the Gforth documentation ( Section 3.27 ) did not work for me ( winXP ) in fact they seemed to open and create files but not write to files properly.
This is the Gforth documentation example that copies the contents of one file to another:
0 Value fd-in
0 Value fd-out
: open-input ( addr u -- ) r/o open-file throw to fd-in ;
: open-output ( addr u -- ) w/o create-file throw to fd-out ;
s" foo.in" open-input
s" foo.out" open-output
: copy-file ( -- )
begin
line-buffer max-line fd-in read-line throw
while
line-buffer swap fd-out write-line throw
repeat ;
I found this example ( http://rosettacode.org/wiki/File_IO#Forth ) which does work. The main problem is that I can't isolate the part that writes to a file and have it still work. The main confusion is that >r doesn't seem to consume TOS as I might expect.
: copy-file2 ( a1 n1 a2 n2 -- )
r/o open-file throw >r
w/o create-file throw r>
begin
pad maxstring 2 pick read-file throw
?dup while
pad swap 3 pick write-file throw
repeat
close-file throw
close-file throw ;
\ Invoke it like this:
s" output.txt" s" input.txt" copy-file
I would be very grateful if someone could explain exactly how the open, create read and write -file words actually work, as my investigation keeps resulting in somewhat bizarre stacks.
Any clues as to why the Gforth examples do not work might help too.
In summary, I want to output from Gforth to a file and so far have been thwarted. Can anyone offer any help?
Thank you Vijay, I think that I understand the example that you gave. However when I try to use something like this ( which I think is similar ):
0 value test-file
: write-test
s" testfile.out" w/o create-file throw to test-file
s" test text" test-file write-line ;
I get ok but nothing is put into the file, have I made a mistake?
It seems that the problem was due to not flushing the relevant buffers or explicitly closing the file. Adding something like
test-file flush-file throw
or
test-file close-file throw
between write-line and ; makes it work. Consequently the Gforth documentation example would have worked had I followed the instructions.
Thanks again Vijay for helping.
I will try to explain how write-line works with this simple example. Here we have a buffer that contains the string " hello", and we want to write that to a file opened with open-output.
buffer 5 fd-out write-line
5 is the length of the buffer. fd-out is the open file handle. A call to write-line will leave an integer result on the stack, the value of which is implementation dependent. More information on the file I/O words could be found in File Access words.
Calling the word throw is optional. It will check the integer value on the top of the stack and based on that value either pops the topmost exception frame from the exception stack or call abort or display an implementation-dependent message giving information about the condition associated with the integer. (Exact details on how throw works could be found in THROW).
You program is incomplete, which makes the question hard to answer.
With prepending
1024 CONSTANT max-line
CREATE line-buffer max-line 3 + ALLOT
and postpending
copy-file
fd-in close-file throw
fd-out close-file throw
and after creating a testfile foo.in gforth the first program works flawlessly on Debian.
Probably your problem is caused by not properly closing the file after writing.