Link to home
Start Free TrialLog in
Avatar of msssyau
msssyau

asked on

File Handling

How can we assign a file that can read and write at same time in trubo Pascal ??
If we use reset, then it will be read only...
On the other hand, if we use rewrite, that will be write only...
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of dbrunton
dbrunton
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yourfather
yourfather

dbrunton is right, u use reset to read a file, and append to add data onto it. keep in mind tho that when u use rewrite, it will erase the previously stored data on the file and overwrite whatever u enter into it after.
Actually there is a method but it doesn't work with text files.  I forget exactly how to do it but you use  SEEK to find the record or spot in the file then you can read it or write it.

I don;t have any of my Pascal books here.

mlmcc
i think RESET  is Good .
it can open file for read and write to it.
I agree RESET will allow reading or writing but I believe once you write to it the rest of the file is lost.  I may be wrong on that but I don't think so.

mlmcc
Sturctured Files...

Type
  The****tyStuffs = Record
    Some****InTheFile: word;
    Another****: Integer;
    AndYesAnother****: String[10];
  End;

Var
  My****tyVar: The****tyStuffs;
  My****tyFile: File of The****tyStuffs;

So you have your **** structured and then you know the size of it... in this case what??? We have a Word that's 2 bytes plus an Integer that's 2 plus a String of ten (plus the SizeOfString-Byte) that's 11, so you have a structure of what??? 15 Bytes right??? or if you don't belive me just do a:

Var
  SizeOfMy****: Byte;

  SizeOfMy****:=SizeOf(My****tVar);

Now to open the ****ty File just do this:

Const
  My****tyFileName: 'My****.Dat';
Var
  ****TheresAnError: Byte;

  Assign(My****tyFile,My****ty);
  {$I-} Reset(My****tyFile,1); {$I+} { Seek by Byte }
  ****TheresAnError:=IOResult;
  If ****TheresAnError <> 0 Then
    { Show Whatever error message you want };
  Seek(My****tyFile,0);

See... You make sure there's no file with your same ****tyName if no file, then Create it with Reset and youre gonna seek the file byte by byte, If there's a file, then just seek the structure data that you wanna Read/Write with:

Var
  Real****tyBytesRead: Byte;

  ****tyRecord:=100 * SizeOfMy**** -1; { Seek Rec 100 }

  BlockRead(My****tyFile,My****tyVar,SizeOfMy****,Real****tyBytesRead);

or
  BlockWrite(My****tyFile,My****tyVar,SizeOfMy****);


Am I Corrrect??? I'm having problems with TP and W2K... Does anyone knows why TP just hangs, on no particular time or thing you do, TP just hangs whenever it please...
Wait... The reason why this does not have a Rewrite function is because I use it to read/Write to a DBF file, so in order to work with such file, you have to have it... so that's why this lines...

  If ****TheresAnError <> 0 Then
   { Show Whatever error message you want };

My proggy exited and showed an error of 'no DBF file found...'
Program ReadWriteFile;
Uses
  WinCrt,WinDos;

Const
  My****tyFileName: String = 'My****.Dat';

Type
 The****tyStuffs = Record
   Some****InTheFile: word;
   Another****: Integer;
   AndYesAnother****: String[10];
 End;


Var
  ****tyCountI, ****tyCountJ: Byte;
  SizeOfMy****: Byte;
  My****tyFile: File;
  ****TheresAnError: Byte;
  Real****tyBytesRead: Word;
  My****tyVar: The****tyStuffs;


Procedure WriteToFile(Num: Byte; ****ToWrite: The****tyStuffs);
Var
  ****tyRecord: Word;
Begin
  ****tyRecord:=(Num - 1) * SizeOfMy****;
  Seek(My****tyFile,****tyRecord);
  BlockWrite(My****tyFile,My****tyVar,SizeOfMy****);
End;

Procedure ReadFromFile(Num: Byte; Var ****tyVar);
Var
  ****tyRecord: Word;
Begin
  ****tyRecord:=(Num - 1) * SizeOfMy****;
  Seek(My****tyFile,****tyRecord);
  BlockRead(My****tyFile,****tyVar,SizeOfMy****,Real****tyBytesRead);
End;

Begin
  ClrScr;
  Randomize;
  SizeOfMy****:=SizeOf(My****tyVar);
  Assign(My****tyFile,My****tyFileName);
  {$I-} Reset(My****tyFile,1); {$I+} { Seek by Byte }
  ****TheresAnError:=IOResult;
  If ****TheresAnError <> 0 Then
    Rewrite(My****tyFile,1);
  Seek(My****tyFile,0);
  WriteLn('Create 20 Records'#10#13);
  For ****tyCountI:=1 to 20 Do
    Begin
      With My****tyVar Do
        Begin
          Some****InTheFile:=Random(10);
          Another****:=Random(10);
          For ****tyCountJ:=1 To 8 Do
            Begin
              AndYesAnother****[****tyCountJ]:=Char(Random(57)+65);
            End;
          AndYesAnother****[9]:=#10;
          AndYesAnother****[10]:=#13;
          AndYesAnother****[0]:=#10;
          Write(Some****InTheFile,'  ',Another****,'  ',My****tyVar.AndYesAnother****);
        End;
      WriteToFile(****tyCountI,My****tyVar);
    End;
  ReadKey;
  ReadFromFile(5,My****tyVar);
  Write('Record Num 5 = ');
  Write(My****tyVar.Some****InTheFile,'  ',My****tyVar.Another****,'  ',My****tyVar.AndYesAnother****);
  ReadKey;
  My****tyVar.Some****InTheFile:=500;
  My****tyVar.Another****:=1000;
  My****tyVar.AndYesAnother****:='Changed'+#10#13;
  Write('Changed to = ');
  Write(My****tyVar.Some****InTheFile,'  ',My****tyVar.Another****,'  ',My****tyVar.AndYesAnother****);
  ReadKey;
  WriteLn;
  WriteLn('Written To File');
  WriteToFile(5,My****tyVar);
  For ****tyCountI:=1 to 20 Do
    Begin
      ReadFromFile(****tyCountI,My****tyVar);
      With My****tyVar Do
        Write(Some****InTheFile,'  ',Another****,'  ',My****tyVar.AndYesAnother****);
    End;
End.
you cheat 8-)

Using BlockRead and BlockWrite is the ONLY way of performing I/O on the same file :D

This is NOT for standard files

Neat cheat, but cheat anyway.

BTW and sadly, Block R/W will disappear in D7 for .Net
whatboy,
You have wondered out ot the lounge and posted verbage that is not acceptable.  Consider this your first and only warning.  It stops here.  My first thought is to give you a temporary suspension but I will monitor after this post.


Computer101
E-E Admin
Would you present this code to your teacher??? what if this is a homework??? at least he has to modify it, and may be, just may be he/she might understand...
And besides I don't see no one complaining but you... And that word ain't that bad...
Can I use Poop instead??? Like:

Type
ThePoopedStuffs = Record
  SomePoopInTheFile: word;
  AnotherPoop: Integer;
  AndYesAnotherPoop: String[10];
End;
VGR:

So you have tools...

Some wood, some nails and a Hammer... Why building a house would be a Cheat???

I don't understand???

You have Seek, BlocdkRead, BlockWrite and SizeOf... just to mention a few...
Easy whatboy

Don't want to lose you from this place.
Listening...
If you are concerned about someone turning in your work as their own, don't give cmplete answers.  Make then adhere to the membership agreement and ask specific questions.  Answer the questions with pseudocode.

mlmcc
Sounds like an attitude problem here.  This is not allowed in American commerce unless the individual holds a PhD.
Sounds like an attitude problem here.  This is not allowed in American commerce unless the individual holds a PhD.
Oh me god... It is right about the Poop... it atracts flies...
And U.N. inspectors...
Well...

i made a DBASE Engine on Pascal just afew years ago...

and the solution is on to parts...

firs check if file exist if dont, then make..
or create... and dont write data at this time
close the file quickly as you can on your code...

after that try to open the file...
yes on your OpenFile Procedure
and set a mode $40... i thing...
If is on share mode is $80...

and make a procedures for append... and read...
and seek...

all this must be make on file with structure...

not apply for a text only....

dont cry i make a WORD Editor (called Editors)
and handle a file with structure just for one line
and work whell...

Mybe this can help you...
Oh brother what a waste of code...
a big L...
Never.

I cut and pasted this thread.
I have changed the default B to an A grade, the defaults will be removed (supposedly) after the rewrite. This occurs with NS, IE5.1 and lower, and Opra.

Thank you,

Wes Lennon
Director of Community Services
Experts Exchange