Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream Arguments Read/Write

Tags:

c#

file-io

In C++, you open a stream like so:

int variable = 45;

ifstream iFile = new ifstream("nameoffile"); // Declare + Open the Stream
       // iFile.open("nameofIle");

iFile >> variable;

iFile.close

I'm trying to understand C# FileStream. The read and write methods require an array and offset and count. This array how big is the array? Do I just give it any size and it will fill it up? If that's the case, how do I go about reading a file with Filestream? How do I know how big of an array I pass in?

like image 316
RoR Avatar asked Dec 21 '22 19:12

RoR


1 Answers

You can simply use StreamReader and StreamWriter wrappers to read and write:

using(StreamReader sr = new StreamReader(fileName))
{
   var value = sr.ReadLine(); // and other methods for reading
}



using (StreamWriter sw = new StreamWriter(fileName)) // or new StreamWriter(fileName,true); for appending available file
{
  sw.WriteLine("test"); // and other methods for writing
}

or do like the following:

StreamWriter sw = new StreamWriter(fileName);
sw.WriteLine("test");
sw.Close();
like image 157
Saeed Amiri Avatar answered Jan 06 '23 16:01

Saeed Amiri