Board index » delphi » Sharing files C++ & pascal

Sharing files C++ & pascal

 have the following problem:
1. I'm writing a C++ dos application that interfaces with a Pascal dos
application via a text file,
2. The C++ app opens the file for writing only (appending) - see code
below,
3. The pascal app opens the file for reading only - see code below,
4. The pascal app seems to lock the file from the C++ app (ie. the
pascal app opens the file exclusively - no sharing) .
(note: the two apps are running on different PC's)

How do I overcome the pascal problem (or is it me C++) ?

Thanx,
Nick.
nick.mce...@dsto.defence.gov.au
--------------------------- C++ ---------------------------
#include           <stdio.h>
#include           <stdlib.h>
#include           <string.h>

void  main()
{
  char             in_line[250];
  FILE             *out_file;

  in_line[0]='a';
  while (in_line[0] != '\0')
   {
     printf ("Enter message : ");
     gets (in_line);

     out_file = fopen("g:\\common\\spc.txt", "a");

  if (out_file)
  {
   if (in_line[0] != '\0') fprintf(out_file, "%s\n", in_line);
   fclose(out_file);
  }
  else
 {
  printf("\tCan't open the file !!!!!\n");
 }
   }

Quote
}

--------------------------- Pascal ---------------------------
 program PCPC;

 var
   f        : text;
   s        : string;
   filename : string;
 begin
   s:='';
   filename := 'g:\common\spc.txt';

   assign (f, filename);
   reset(f);

   while {(not eof(f))}  (s[1]<>'Q') do
   begin
     readln(f, s);
     if s<>'' then
       writeln('Received >'+s+'<');
   end;
   close(f);
 end.

 

Re:Sharing files C++ & pascal


Quote
Nick McEvoy <nick.mce...@dsto.defence.gov.au> wrote:
> have the following problem:
>1. I'm writing a C++ dos application that interfaces with a Pascal dos
>application via a text file,
>2. The C++ app opens the file for writing only (appending) - see code
>below,
>3. The pascal app opens the file for reading only - see code below,
>4. The pascal app seems to lock the file from the C++ app (ie. the
>pascal app opens the file exclusively - no sharing) .
>(note: the two apps are running on different PC's)

>How do I overcome the pascal problem (or is it me C++) ?

The Open procedure in my FILES Unit available from Garbo or at
http://users.leading.net/~rdonais/tpascal.htm will allow your
TP/BP program to open a text file as ReadOnly+DenyNone.

The archive contains TP/BP 4.0-7.0 compatible source so you can
not only use the unit as-is, but you can cut-'n-paste or modify
the functions and procedures as you see fit.

I checked using share & two DOS boxes under Win 3.1 and found
that readln will return an empty string at EOF and automatically
pick up subsequent lines as they are written to the file.
Likewise, EOF will report TRUE at EOF and will automatically
become FALSE whenever the file is extended by the other program.

Since this was all on a single CPU using a non-preemptive OS I
really didn't expect any problems.  However, it will be up to
you to implement appropriate file locking protocols on a
network.

One crude network possibility would be to have each program open
the file only when they actually needed to do file i/o.  The
TP/BP program could use FindFirst to determine the file size.
Whenever it changed, it could use Reset within a loop until
IOResult = 0, then use FILES.SeekText to position the file to
the previous EOF.  After reading to the new EOF, close the file
and again wait until FindFirst reports a new file size.

Happy Holidays.

    ...red

Other Threads