From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,FREEMAIL_FROM, INVALID_MSGID,SUBJ_ALL_CAPS autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a5c3c5d6abe552c5 X-Google-Attributes: gid103376,public From: johnherro@aol.com (John Herro) Subject: Re: THE OLD IN OUT, IN OUT Date: 1996/06/05 Message-ID: <4p349u$h8b@newsbf02.news.aol.com>#1/1 X-Deja-AN: 158533612 sender: root@newsbf02.news.aol.com references: <4ou662$951@fang.dsto.defence.gov.au> organization: America Online, Inc. (1-800-827-6364) newsgroups: comp.lang.ada Date: 1996-06-05T00:00:00+00:00 List-Id: Barney Rubble wrote: > Can anyone help me with this problem? > I'm trying to modify (i.e. read and write > to) a text file. As far as I know, you can > only open a file as only In_File or > Out_File, and Out_File always creates > a blank file. I tried to open two copies > of a file, one as an In_File and one as an > Out_File, simultaneously, but it came up > with a Use_Error when I tried to open > the second. Is there (an easy) way of > doing this? You're right, there's no *easy* way to modify a text file, and the Use_Error was to be expected. In Ada 95 you can Open a text file in Append_File mode. In Ada 83, Append_File mode isn't available, but in some implementations you can accomplish the same thing by using the Form parameter. If you have an Ada 83 compiler, you'll have to consult the compiler documentation, as different implementations of Ada 83 aren't consistent in their handling of (or even allowing) a Form parameter. However, so far I've only talked about *appending* to a file. You can't *directly* modify what's already written. But there are several techniques you can use. First, if you have enough memory, you can read the entire file into memory (possibly into a linked list), modify memory, and then write the entire file out. Or, your problem may permit reading the file a chunk (say, 50 lines) at a time, modifying it, and then writing the chunk to a new file, all in a loop. At the end of your program, you can delete the old file and rename the new. With some implementations of Ada (for example, Open Ada for DOS), you *could* open a text file with Direct_IO instantiated for Character, and do random reads and writes a character at a time. It's inefficient and messy, but I've done it. With other implementations of Ada (for example, Vax Ada for VMS), this won't work, because files opened with an instantiation of Direct_IO have different attributes from text files. In my Ada Tutor program, available for download at the WWW and FTP sites below my signature, there's an example of a simple line editor that works by reading the entire file into a linked list, modifying it, and then writing out the entire file. I don't think this is what you were hoping to hear, but I hope it's better than nothing. Let us know if we can be of further help. - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor