This question was asked in part before, but I am trying to rephrase it to avoid subterfuge. It is not clear how internal and external files are named by File_type. If the internal file name is closed, then the external file is automatically closed, but so what. Ada95 file i/o was not well thought out in my opinion.
BEGIN
Close( To_string( file_name_unb_str)) ; – barfs, but why?
EXCEPTION
WHEN Name_error => null ;
WHEN Others => null ;
END ;
The idea is to emulate TB’s CLOSE #3, where #3 is a channel that may or may not exist as yet, and if it does not exist there is no exception raised to close it. If it is open, then it is closed.
As-written?
Well, because Close takes a File_Type and you’re feeding it a String.
Interesting conclusion.
I’m going to ask why you think that it’s not well thought out.
It looks like you’re not understanding types and/or parameters, TBH.
Spec:
pragma Ada_95;
with
Ada.Text_IO;
package Example is
pragma Elaborate_Body ( Sitemap );
type Handle(<>) is private;
function Safe_Create(Name : String;
Mode : Ada.Text_IO.File_Mode:= Ada.Text_IO.Out_File
) return Handle;
procedure Safe_Close( Object : in out Ada.Text_IO.File_Type );
private
type Handle is access Ada.Text_IO.File_Type;
end Example;
Body:
pragma Ada_95;
package body Example is
function Safe_Create(Name : String;
Mode : Ada.Text_IO.File_Mode:= Ada.Text_IO.Out_File
) return Handle is
use Ada.Text_IO;
temp : Handle:= Handle'(new File_Type);
begin
begin
Open(Temp.all, Mode, Name);
exception
when Ada.Text_IO.Name_Error =>
Create(Temp.all, Mode, Name);
end;
return temp;
end Safe_Create;
procedure Safe_Close(Object: in out Ada.Text_IO.File_Type) is
use Ada.Text_IO;
begin
Close( Object );
exception
when Ada.Text_IO.Status_Error => null;
end Safe_Close;
FILE : Constant String:= "STEVE";
Temp : Ada.Text_IO.File_Type;
P: Handle;
begin
P:= Safe_Create( FILE, Ada.Text_IO.IN_FILE );
Safe_Close( Temp );
null;
end Example;
I would expect you to understand at least what’s going on, except you’ve been rather unwilling to help us help you. Not only did you fail to format your code, you didn’t include error-messages; also, you refused to read the documentation on the standard library… and your comments are rather indicative that you are not trying to learn the actual language but merely have some mental syntax-transformations on some other language. — All combined, you’re going to be fighting the language and the compiler rather than letting it help you.
" Close takes a File_Type and you’re feeding it a String ."
That is correct. A problem not apparent from my example is that whack_a_mole_file : File_type, an internal mental object, if so supplied in Close( whack_a_mole_file) bears no resemblance to my external physical object of file_name_unb_str. In fact, all I care about is my file_name_unb_str to make sure it’s closed before I attempt to open it. The subsequent typing is not informative to me as a practitioner and amounts to contempt for my project and criticizing the student.
“[quote=“m8vl4, post:1, topic:1226”]Ada95 file i/o was not well thought out[/quote]
Interesting conclusion.
I’m going to ask why you think that it’s not well thought out.
It looks like you’re not understanding types and/or parameters, TBH.”
So you ask me a question then proffer a conclusion before my reply, and why wouldn’t you be honest.
Not well thought out is shown by Cohen 1996 being forced to try to explain file i/o for the audience of Ada as a second language in 66 pages, and hence a predictable response here I was trying to avoid.
The BASICs’ channels correspond to Ada File_Type’s, so you would have the same problem trying to close a string in BASIC. Try some self-criticism, it will be helpful!
It isn’t a string either in Ada.
What I mean is that you use a variable of type File_Type in Ada like you use a channel number in various BASICs.
If you understand this correspondance and how it works in BASIC, you will understand how it works in Ada. The commands are identical or very similar, and are placed at the same place in all languages.
You’re awfully offensive for someone who’s not even trying. You’re the first person to find this difficult, so maybe you’re dyslexic ? Try humility next time. If you reach the end of a declarative block where a file_type element is declared and Open-ed without Close-ing it, the behavior is not specified, meaning don’t do that.
Blockquote
You’re awfully offensive for someone who’s not even trying. You’re the first person to find this difficult, so maybe you’re dyslexic ? Try humility next time. If you reach the end of a declarative block where a file_type element is declared and Open-ed without Close-ing it, the behavior is not specified, meaning don’t do that .
Blockquote
First an insult, then an admission that Ada has an egregious “behavior is not specified”.
It isn’t a string either in Ada.
What I mean is that you use a variable of type File_Type in Ada like you use a channel number in various BASICs.
If you understand this correspondance and how it works in BASIC, you will understand how it works in Ada. The commands are identical or very similar, and are placed at the same place in all languages.
I’m not giving you a conclusion, I’m telling you what it looks like from what you’ve supplied is that you have a fundamental misunderstanding of types (which are absolutely fundamental to Ada), and even parameters (which is fundamental to programming).