No such file or directory

Viewed 1543

I am coding a hashing program in ada and using direct io to read and write to/from a file. I am trying to read from a file that is in the same folder as the executable as it should be but still raising the exception. Any ideas as to why its still raising this exception? adb showing exception driver ads file

2 Answers

The location of the executable has no impact on the interpretation of the names of files to be opened or created. The relevant issue is the current working directory (or folder, if you will) of the process that executes the program. In the common OSes, for a file to be found based on its file-name alone (without any directory path), the file must lie in the current working directory.

You seem to be executing the program from within some IDE, right? Then the IDE probably defines the current working directory to be used when the IDE executes the program. Do you know how the IDE does that, and can you override the default within the IDE? If not, I suggest that you execute the program from the shell command line and manually set the current working directory as needed, in that shell window, using the "cd" command before executing the program.

You could use Ada.Directories (ARM A.16) to work out the location of the data file from the location of the executable:

use Ada.Directories;
Program_Name     : constant String := Ada.Command_Line.Command_Name;
Complete_Name    : constant String := Full_Name (Program_Name);
Full_Directory   : constant String := Containing_Directory (Complete_Name);
Source_File_Name : constant String
  :=  Compose (Containing_Directory => Full_Directory,
               Name                 => "foo",
               Extension            => "txt");

Note, the use Ada.Directories meant I had to be a bit 'creative' about variable names; without it, I could say e.g.

Full_Name : constant String 
  := Ada.Directories.Full_Name (Program_Name);
Related