| 
       Reading and Writing  | 
    Top Previous Next | 
| 
 What is translated > Reading and Writing Delphi has Stream classes to read and write files similar to those in C#. But there are also an classic, non-object oriented Pascal routines for this purpose. For this classic approach there are three file types, which have no counterpart in C# 
 
 
 
 
 Delphi2C# tries to convert this classic approach to the C# object-oriented approach. For all three file types in C# a Stream is created at first. In a second step in dependence of the different Delphi file types different kinds of Streams are created from the first basic stream. For example a StreamWriter will be created, if ASCII data shall be written to a Text file: 
 
 var myFile : Text; 
 begin 
 AssignFile(myFile, 'Test.txt'); Rewrite(myFile); 
 -> 
 Stream myFile_stream; 
 myFile_stream = new FileStream("Test.txt", FileMode.Create); using (StreamWriter myFile = new StreamWriter(myFile_stream)) { ... } 
 
 In Delphi the access mode of these file types are specified by the FileMode variable. In C# there also exists a FileMode, but this variable specifies how to open a file, eg. create a new file or append to an existing file. Therefore the Delphi FileMode variable cannot be defined in the C# code. In translated code the Delphi FileMode is passed as explicit parameter to the file opening commands, "FileMode.Create" in the example above. "FileMode.CreateOrOpen" is the FileMode for readers.The kind of opening a file in Delphi is determined by the command which is used to open the file. The ReWrite command and the Reset command are truncating an existing file or creating a new file, whereas the Append command opens a file to add output to the existing content of a file. 
 The default FileMode in Delphi is fmOpenReadWrite (=2). but in C# a stream cannot have read and write access at the same time. Delphi2C# therefore looks at the subsequent use of the stream. If writing operations are following a Writer is created else a Reader is created. If the Delphi code really reads and writes to the file without resetting it, the translation will fail. 
 If a Write function is called with a file as first parameter, the output will be written into that file. Otherwise the Output is written to the console: 
 WriteLn(myFile, 'Hello'); WriteLn('Hello'); 
 -> 
 myFile.WriteLine("Hello"); Console.WriteLine("Hello"); 
 
 The same for Read-functions: 
 Read(myFile, Letter); Read(Letter); 
 -> 
 Letter = (char) myFile.Read(); Letter = (char) Console.Read(); 
 
 
 For Files and Files of a type Delphi2C# creates BinaryReader and BinaryWriter. Delphi2C# only converts uses of File of [builtin type] An automatic treatment of files of records might partly be possible, but hasn't be done so far. 
 var myWord : WORD; myFile : File of WORD; 
 begin AssignFile(myFile, 'Test.cus'); Rewrite(myFile); 
 While not Eof(myFile) do begin Read(myFile, myWord); end; 
 CloseFile(myFile); 
 -> 
 ushort myWord = 0; Stream myFile_stream; 
 myFile_stream = new FileStream("Test.cus", FileMode.OpenOrCreate); using (BinaryReader myFile = new BinaryReader(myFile_stream, System.Text.Encoding.ASCII)) { 
 while(!(myFile.PeekChar() < 0)) { myWord = (ushort) myFile.ReadInt16(); } myFile.Close(); } // using 
 
 
 Delphi2C# also converts Rename commands. The width and precision arguments in write operations aren't converted correctly yet. 
 
 
 
 
 
 
 
 
 
  | 
| 
       This page belongs to the Delphi2C# Documentation  | 
    Delphi2C# home Content |