|
Binary Input and Output |
Top Previous Next |
|
What is translated > File Input and Output > Binary Input and Output READING AND WRITING TYPED FILES
TypedFile<T> uses Read and Write.
Each call processes exactly one complete value of type T.
Writing a value:
ushort value = 234;
AssignFile(myFile, "Words.dat"); Rewrite(myFile); Write(myFile, value); CloseFile(myFile);
Reading a value:
Reset(myFile); Read(myFile, ref value); CloseFile(myFile);
A normal reading loop tests Eof before Read:
while (!Eof(myFile)) { Read(myFile, ref value); }
Read throws EndOfStreamException if the file does not contain one complete record.
The TypedFile<T> constructor and CreateRecord method initialize the record size from the serialized size of T.
BLOCKREAD AND BLOCKWRITE
UntypedFile uses BlockRead and BlockWrite.
The number of bytes processed is the record count multiplied by the record size selected by Reset or Rewrite.
Example with a record size of one byte:
UntypedFile myFile = UntypedFile.CreateRecord();
byte[] buffer = new byte[256]; int count = 0;
AssignFile(myFile, "Data.bin"); Rewrite(myFile, 1);
BlockWrite( myFile, buffer, buffer.Length, out count);
CloseFile(myFile);
The overload with an output parameter returns the number of records that were actually processed:
BlockRead( myFile, buffer, buffer.Length, out count);
BlockWrite( myFile, buffer, buffer.Length, out count);
Overloads without an output parameter perform the same operation but discard the returned record count.
FILEMODE
The Delphi FileMode variable controls how typed and untyped files are opened by Reset.
The access values are:
fmOpenRead fmOpenWrite fmOpenReadWrite
The sharing values are:
fmShareCompat fmShareExclusive fmShareDenyWrite fmShareDenyRead fmShareDenyNone
Access and sharing values can be combined.
Example:
FileMode = fmOpenRead | fmShareDenyWrite; Reset(myFile);
FileMode does not control Rewrite. Rewrite creates or replaces the file and opens it for writing.
Delphi2CSharp automatically qualifies references to the Delphi FileMode variable as SystemInterface.FileMode. This avoids a name conflict with the System.IO.FileMode enumeration used by the .NET file classes.
RENAMING FILES
Rename changes the physical name of a file associated with a Delphi file variable.
The file must first be associated with AssignFile:
AssignFile(myFile, "OldName.txt");
The file must be closed before Rename is called:
Rename(myFile, "NewName.txt");
After a successful Rename call, the file variable is associated with the new file name.
Rename supports string and PChar file names.
Examples:
Rename(myFile, "NewName.txt");
PChar newName = new PChar("NewName.txt"); Rename(myFile, newName); |
|
This page belongs to the Delphi2C# Documentation |
Delphi2C# home Content |