Writng objects to file
-
Hello All!! I want to write the objects on file. I know how to do this in C++ but i m not geting the equvalent way in .net e.g pseudocode is like
class MyClass{ int a; int b; string c; } MyClass obj = new MyClass(); obj.a=0; obj.b=1; obj.c = "can i do this";
In C++ I used to do it likeofstream out(fileName,ios::binary | ios::trunc); out.write((char*)obj,sizeof(*obj));
But what is equivalent of ofstream in .net Thanx in Advance. sorry for my bad English. -
Hello All!! I want to write the objects on file. I know how to do this in C++ but i m not geting the equvalent way in .net e.g pseudocode is like
class MyClass{ int a; int b; string c; } MyClass obj = new MyClass(); obj.a=0; obj.b=1; obj.c = "can i do this";
In C++ I used to do it likeofstream out(fileName,ios::binary | ios::trunc); out.write((char*)obj,sizeof(*obj));
But what is equivalent of ofstream in .net Thanx in Advance. sorry for my bad English.Not gonna try like I am an expert by an means here but you should look at this article here on Codeproject http://www.codeproject.com/aspnet/fileupload.asp?target=Upload%7CFile[^] about uploading files with ASP.net/C#, I know it sounds off topic but it shows reading in and writing out a file using the FileStream class. Maybe it will help you on the right track?
-
Not gonna try like I am an expert by an means here but you should look at this article here on Codeproject http://www.codeproject.com/aspnet/fileupload.asp?target=Upload%7CFile[^] about uploading files with ASP.net/C#, I know it sounds off topic but it shows reading in and writing out a file using the FileStream class. Maybe it will help you on the right track?
Thanx Kluch!! Well! I can find the overloaded methods to write bytes, strings, integers on files using different streams such as filestream, binarywriter etc but i want to write my custom class object on file, as mentioned in VC++ code in my question. It is quite easy in VC++ and there must be an equivalent function in C# as well. But I coudln't trace it out. :confused: Regards Farrukh sorry for my bad English.
-
Hello All!! I want to write the objects on file. I know how to do this in C++ but i m not geting the equvalent way in .net e.g pseudocode is like
class MyClass{ int a; int b; string c; } MyClass obj = new MyClass(); obj.a=0; obj.b=1; obj.c = "can i do this";
In C++ I used to do it likeofstream out(fileName,ios::binary | ios::trunc); out.write((char*)obj,sizeof(*obj));
But what is equivalent of ofstream in .net Thanx in Advance. sorry for my bad English.Writing objects into a stream is called "Serialization", C# contains two different foramtters: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter for binary formatting, and System.Runtime.Serialization.Formatters.Soap.SoapFormatter for XML formatting. The SerializableAttribute marks a class as serializable: [Serializable()] public class MyClass{ public int a; public int b; public string c; } To write an object of that class to a file stream, you can use the Serialize-method: BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(myFileName, FileMode.Create); formatter.Serialize(stream, obj); Later you can read teh object from the file with Deserialize(): MyClass obj = (MyClass)formatter.Deserialize(stream); Itanium wrote: sorry for my bad English. Sorry for my even worse English. Have I understood the question, or is this an off-topic posting? ;)
-
Writing objects into a stream is called "Serialization", C# contains two different foramtters: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter for binary formatting, and System.Runtime.Serialization.Formatters.Soap.SoapFormatter for XML formatting. The SerializableAttribute marks a class as serializable: [Serializable()] public class MyClass{ public int a; public int b; public string c; } To write an object of that class to a file stream, you can use the Serialize-method: BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(myFileName, FileMode.Create); formatter.Serialize(stream, obj); Later you can read teh object from the file with Deserialize(): MyClass obj = (MyClass)formatter.Deserialize(stream); Itanium wrote: sorry for my bad English. Sorry for my even worse English. Have I understood the question, or is this an off-topic posting? ;)