comp.lang.ada
 help / color / mirror / Atom feed
* newbie binary file I/O question
@ 1995-03-03 16:31 Dolores Shaffer
  1995-03-04 16:01 ` Tore Joergensen
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dolores Shaffer @ 1995-03-03 16:31 UTC (permalink / raw)


Hi,

I am using an old Telesoft Ada compiler on a Sun workstation.  I have
a binary file that I would like to open and read one byte at a time.
If the code asks the user for the file name and then stores that in
a string variable, and the code passes that variable to the open 
procedure, I get a NAME_ERROR and the code bombs.  If the code
passes the name of the binary file in quotes, it works.  I am
able to use text_io.open to open text files whose names are passed to
open in a string variable.  Any suggestions?

Here is part of the code:

with text_io;
with sequential_io;
procedure nextstep is

Package byte_io is new sequential_io(intbyte);

din_file: byte_io.file_type;
value:intbyte;
name_of_file: string(1..80);
name_length : natural;

begin
    text_io.put_line("enter name of file to open");
    text_io.get_line(name_of_file,name_length);

    byte_io.open(din_file, byte_io.file_mode'first, name=>name_of_file);
--IT BOMBS HERE!!
--but byte_io.open(din_file, byte_io.file_mode'first, "filename"); works



-- 
D. A. Shaffer, shaffer@nvl.army.mil
Night Vision & Electronic Sensors Directorate 
DISCLAIMER:  facts & opinions expressed are not necessarily those
of Night Vision & Electronic Sensors Directorate.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: newbie binary file I/O question
  1995-03-03 16:31 newbie binary file I/O question Dolores Shaffer
@ 1995-03-04 16:01 ` Tore Joergensen
  1995-03-05 23:04 ` Keith Thompson
  1995-03-14 16:18 ` Michael M. Bishop
  2 siblings, 0 replies; 4+ messages in thread
From: Tore Joergensen @ 1995-03-04 16:01 UTC (permalink / raw)


Dolores Shaffer (shaffer@oliver) wrote:
: Hi,

: I am using an old Telesoft Ada compiler on a Sun workstation.  I have
: a binary file that I would like to open and read one byte at a time.
: If the code asks the user for the file name and then stores that in
: a string variable, and the code passes that variable to the open 
: procedure, I get a NAME_ERROR and the code bombs.  If the code
: passes the name of the binary file in quotes, it works.  I am
: able to use text_io.open to open text files whose names are passed to
: open in a string variable.  Any suggestions?

: Here is part of the code:

: with text_io;
: with sequential_io;
: procedure nextstep is

: Package byte_io is new sequential_io(intbyte);

: din_file: byte_io.file_type;
: value:intbyte;
: name_of_file: string(1..80);
Try name_of_file: string(1..80) := (others => ' ');
and if that doesn't work, try

: name_length : natural;

: begin
:     text_io.put_line("enter name of file to open");
:     text_io.get_line(name_of_file,name_length);

:     byte_io.open(din_file, byte_io.file_mode'first, name=>name_of_file);
      byte_io.open(din_file, byte_io.file_mode'first, name=>name_of_file(1..name_length));
: --IT BOMBS HERE!!
: --but byte_io.open(din_file, byte_io.file_mode'first, "filename"); works



: -- 
: D. A. Shaffer, shaffer@nvl.army.mil
: Night Vision & Electronic Sensors Directorate 
: DISCLAIMER:  facts & opinions expressed are not necessarily those
: of Night Vision & Electronic Sensors Directorate.

--
______________________________________________________________________
Tore B. Joergensen,    |    e-mail:     tore@lis.pitt.edu
a norwegian student    |    snail-mail: 2201 Pittockstr.
a long way from home.  |                Pittsburgh, 15217 PA
                       |    web:        http://www.pitt.edu/~tojst1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: newbie binary file I/O question
  1995-03-03 16:31 newbie binary file I/O question Dolores Shaffer
  1995-03-04 16:01 ` Tore Joergensen
@ 1995-03-05 23:04 ` Keith Thompson
  1995-03-14 16:18 ` Michael M. Bishop
  2 siblings, 0 replies; 4+ messages in thread
From: Keith Thompson @ 1995-03-05 23:04 UTC (permalink / raw)


In <D4vHw0.LB@nvl.army.mil> shaffer@oliver (Dolores Shaffer) writes:
> I am using an old Telesoft Ada compiler on a Sun workstation.  I have
> a binary file that I would like to open and read one byte at a time.
> If the code asks the user for the file name and then stores that in
> a string variable, and the code passes that variable to the open 
> procedure, I get a NAME_ERROR and the code bombs.  If the code
> passes the name of the binary file in quotes, it works.  I am
> able to use text_io.open to open text files whose names are passed to
> open in a string variable.  Any suggestions?
[...]
> name_of_file: string(1..80);
> name_length : natural;
> 
> begin
>     text_io.put_line("enter name of file to open");
>     text_io.get_line(name_of_file,name_length);
> 
>     byte_io.open(din_file, byte_io.file_mode'first, name=>name_of_file);
> --IT BOMBS HERE!!
> --but byte_io.open(din_file, byte_io.file_mode'first, "filename"); works

The contents of the variable Name_Of_File past Name_Length are undefined.
The name the user entered is in the slice
    Name_Of_File(1 .. Name_Length)
Use that instead of just Name_Of_File in the Open call.

Someone else suggested initializing Name_Of_File to all blanks; this is
unlikely to work.  Under Unix, for example, a blank is a valid character
in a file name; "foo" and "foo " are legal and distinct file names.

Digression:
    To be more precise about what's going on, change the variable Name_Length
    to Name_Last, and use the slice
	Name_Of_File(Name_Of_File'First .. Name_Last)
    Not all strings are one-based; for strings that aren't one-based,
    the 'Last is not the same as the 'Length.  In this particular case,
    you know Name_Of_File is one-based because you declared it that way,
    but it's still better to be explicit.
end Digression;

-- 
Keith Thompson (The_Other_Keith)  kst@thomsoft.com (kst@alsys.com still works)
TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718
That's Keith Thompson *with* a 'p', Thomson Software Products *without* a 'p'.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: newbie binary file I/O question
  1995-03-03 16:31 newbie binary file I/O question Dolores Shaffer
  1995-03-04 16:01 ` Tore Joergensen
  1995-03-05 23:04 ` Keith Thompson
@ 1995-03-14 16:18 ` Michael M. Bishop
  2 siblings, 0 replies; 4+ messages in thread
From: Michael M. Bishop @ 1995-03-14 16:18 UTC (permalink / raw)


In article <D4vHw0.LB@nvl.army.mil>, Dolores Shaffer <shaffer@oliver> wrote:
>procedure nextstep is
>
>Package byte_io is new sequential_io(intbyte);
>
>din_file: byte_io.file_type;
>value:intbyte;
>name_of_file: string(1..80);
>name_length : natural;
>
>begin
>    text_io.put_line("enter name of file to open");
>    text_io.get_line(name_of_file,name_length);
>
>    byte_io.open(din_file, byte_io.file_mode'first, name=>name_of_file);
>--IT BOMBS HERE!!
>--but byte_io.open(din_file, byte_io.file_mode'first, "filename"); works

You might want to try this:

     byte_io.open (din_file, byte_io.file_mode'first, 
         name => name_of_file(1 .. name_length));

What is probably happening in the code that raises name_error is that
the slice name_of_file(name_length + 1 .. name_of_file'last) (i.e., the
part of name_of_file that wasn't filled in by get_line) has some junk
characters in it and the OS is thinking that those junk characters
constitute part of the file name. So the slice name_of_file(1 ..
name_length) references only that part of name_of_file that the user
entered.

Hope this works...
-- 
| Mike Bishop              | The opinions expressed here reflect    |
| bishopm@source.asset.com | those of this station, its management, |
| Member: Team Ada         | and the entire world.                  |



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-03-14 16:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-03 16:31 newbie binary file I/O question Dolores Shaffer
1995-03-04 16:01 ` Tore Joergensen
1995-03-05 23:04 ` Keith Thompson
1995-03-14 16:18 ` Michael M. Bishop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox