IO question

  • Thread starter Thread starter Bor de Wolf
  • Start date Start date
B

Bor de Wolf

Please tell me why I am not getting out what I put in.

FileStream nrIn = new
FileStream("DataCount.prl",FileMode.OpenOrCreate,FileAccess.Read) ;
BinaryReader inr = new BinaryReader(nrIn);
recordNumber = inr.Read();
nrIn.Close();
Console.WriteLine("The current recordnumber is{0}",recordNumber.ToString());
Console.Write("Enter record number: ");
recordNumber = Console.Read();
FileStream nrOut = new
FileStream("DataCount.prl",FileMode.Create,FileAccess.Write) ;
BinaryWriter outW = new BinaryWriter(nrOut);
outW.Write(recordNumber);
nrOut.Close();
 
Bor de Wolf said:
Please tell me why I am not getting out what I put in.

FileStream nrIn = new
FileStream("DataCount.prl",FileMode.OpenOrCreate,FileAccess.Read) ;
BinaryReader inr = new BinaryReader(nrIn);
recordNumber = inr.Read();
nrIn.Close();
Console.WriteLine("The current recordnumber is{0}",recordNumber.ToString());
Console.Write("Enter record number: ");
recordNumber = Console.Read();
FileStream nrOut = new
FileStream("DataCount.prl",FileMode.Create,FileAccess.Write) ;
BinaryWriter outW = new BinaryWriter(nrOut);
outW.Write(recordNumber);
nrOut.Close();

Related to your earlier question. Console.Read() reads one character. This
is very different from read in Pascal.
 
int Console.Read();
Reads the next character from the standard input stream.

[Visual Basic]
Public Shared Function Read() As Integer
[C#]
public static int Read();
[C++]
public: static int Read();
[JScript]
public static function Read() : int;
Return Value
The next character from the input stream, or negative one (-1) if no more
characters are available.

Exceptions
Exception Type Condition
IOException An I/O error occurred.

Remarks
This method will not return until the read operation is terminated; for
example, by the user pressing the enter key. If data is available, the input
stream contains what the user entered, suffixed with the environment
dependent newline character.

Example
[Visual Basic]
Dim i As Integer
Dim c As Char
While True
i = Console.Read()
If i = - 1 Then
Exit While
End If
c = Microsoft.VisualBasic.Chr(i)
Console.WriteLine("Echo: {0}", c)
End While
Console.WriteLine("Done")
Return 0
[C#]
int i;
char c;
while (true)
{
i = Console.Read ();
if (i == -1) break;
c = (char) i;
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;
[JScript]
var i : int;
var c : char;
while (true)
{
i = Console.Read ();
if (i == -1) break;
c = char(i);
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;
[C++] No example is available for C++. To view a Visual Basic, C#, or
JScript example, click the Language Filter button in the upper-left corner
of the page.

Requirements
 
Back
Top