comp.lang.ada
 help / color / mirror / Atom feed
* Help needed; Saving access types with sequential_io?
@ 2003-02-24 14:19 khendon
  2003-02-24 17:58 ` Rodrigo García
  2003-02-25  3:24 ` Steve
  0 siblings, 2 replies; 3+ messages in thread
From: khendon @ 2003-02-24 14:19 UTC (permalink / raw)


Hi all

I'm a novice in ADA programming, and programming in general. For a
school project I'm currently working on I'm supposed to create a
database that needs to have saving/loading functionality. It's all
very simple stuff, but I'm stuck at the moment, since I don't really
know how to save the different access types I'm using. The
informations is saved in records:

type info;
type a_info is access info;
type a_string is access string;
type info is
  record
    a: a_string;
    b: a_string;
  end record;

First, I tried just saving the entire record. That didn't work, my
guess is because it contains access types, and whatever they're
currently pointing at disapperas when I shut down the program. So, I
then thought of saving each string separately. That seemed to work, at
least the data got saved as it was supposed to. Unfortunately, I'm
having trouble loading the data from the file into new records that
will then be added to the database.

The only way I can think of is to load the data into temporary string,
and then add those strings to the record. The problem with this is
that those temporary strings need to have a defined length, so when I
add them to the record, they will contain lots of garbage I just don't
want there. That's the whole point of using access types in the record
in the first place, to be able to save strings of different lengths
depending on user input.

If anyone could provide any help I would be most grateful. 

Regards, Isak



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

* Re: Help needed; Saving access types with sequential_io?
  2003-02-24 14:19 Help needed; Saving access types with sequential_io? khendon
@ 2003-02-24 17:58 ` Rodrigo García
  2003-02-25  3:24 ` Steve
  1 sibling, 0 replies; 3+ messages in thread
From: Rodrigo García @ 2003-02-24 17:58 UTC (permalink / raw)


   I would use streams. See if the following code helps you:

with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Text_IO;

procedure Data_Save is
    type A_String is access String;

    type Data_Record is record
       A : A_String;
    end record;

    Saved : Data_Record;
    Loaded : Data_Record;
    F : File_Type;
    S : Stream_Access;
begin
    Create (F, Name => "serialized.txt");
    S := Stream (F);
    Saved.A := new String'("Data saved");
    String'Output (S, Saved.A.all);
    Close (F);

    Open (F, In_File, "serialized.txt");
    S := Stream (F);
    Loaded.A := new String'(String'Input (S));
    Close (F);
    Ada.Text_IO.Put_Line (Loaded.A.all);
end Data_Save;

Rodrigo

khendon wrote:
> Hi all
> 
> I'm a novice in ADA programming, and programming in general. For a
> school project I'm currently working on I'm supposed to create a
> database that needs to have saving/loading functionality. It's all
> very simple stuff, but I'm stuck at the moment, since I don't really
> know how to save the different access types I'm using. The
> informations is saved in records:
> 
> type info;
> type a_info is access info;
> type a_string is access string;
> type info is
>   record
>     a: a_string;
>     b: a_string;
>   end record;
> 
> First, I tried just saving the entire record. That didn't work, my
> guess is because it contains access types, and whatever they're
> currently pointing at disapperas when I shut down the program. So, I
> then thought of saving each string separately. That seemed to work, at
> least the data got saved as it was supposed to. Unfortunately, I'm
> having trouble loading the data from the file into new records that
> will then be added to the database.
> 
> The only way I can think of is to load the data into temporary string,
> and then add those strings to the record. The problem with this is
> that those temporary strings need to have a defined length, so when I
> add them to the record, they will contain lots of garbage I just don't
> want there. That's the whole point of using access types in the record
> in the first place, to be able to save strings of different lengths
> depending on user input.
> 
> If anyone could provide any help I would be most grateful. 
> 
> Regards, Isak





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

* Re: Help needed; Saving access types with sequential_io?
  2003-02-24 14:19 Help needed; Saving access types with sequential_io? khendon
  2003-02-24 17:58 ` Rodrigo García
@ 2003-02-25  3:24 ` Steve
  1 sibling, 0 replies; 3+ messages in thread
From: Steve @ 2003-02-25  3:24 UTC (permalink / raw)


"khendon" <lundin_isak@hotmail.com> wrote in message
news:1946b526.0302240619.695b865a@posting.google.com...
> Hi all
[snip]
> The only way I can think of is to load the data into temporary string,
> and then add those strings to the record. The problem with this is
> that those temporary strings need to have a defined length, so when I
> add them to the record, they will contain lots of garbage I just don't
> want there. That's the whole point of using access types in the record
> in the first place, to be able to save strings of different lengths
> depending on user input.
>
> If anyone could provide any help I would be most grateful.
>

Two common methods for handling this common problem:
  1. Precede the data in the string with a fixed size number giving the
number of characters in the string.

  Example:
    For a record containing the strings "Three" and "Four" the file would
contain:
  5 'T' 'h' 'r' 'e' 'e' 4 'F' 'o' 'u' 'r'

  2. Include a special character as a string separator (C uses nul).

  Example:
    For a record containing the strings "Three" and "Four" the file would
contain:
  'T' 'h' 'r' 'e' 'e' 0 'F' 'o' 'u' 'r' 0

Personally I prefer the first approach since you don't have to scan each
character while it is being read, and no characters are off limits for being
contained in the string.

If you are not writing a program that is time critical (few programes really
are), I would suggest using Ada.Strings.Unbounded_String instead of handling
your own dynamic allocation and deallocation.

I hope this helps,
Steve
(The Duck)

> Regards, Isak





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

end of thread, other threads:[~2003-02-25  3:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-24 14:19 Help needed; Saving access types with sequential_io? khendon
2003-02-24 17:58 ` Rodrigo García
2003-02-25  3:24 ` Steve

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