Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Writng objects to file

Writng objects to file

Scheduled Pinned Locked Moved C#
csharpc++iostutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Itanium
    wrote on last edited by
    #1

    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 like ofstream 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.

    M C 2 Replies Last reply
    0
    • I Itanium

      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 like ofstream 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.

      M Offline
      M Offline
      MKlucher
      wrote on last edited by
      #2

      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?

      I 1 Reply Last reply
      0
      • M MKlucher

        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?

        I Offline
        I Offline
        Itanium
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • I Itanium

          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 like ofstream 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.

          C Offline
          C Offline
          Corinna John
          wrote on last edited by
          #4

          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? ;)

          I 1 Reply Last reply
          0
          • C Corinna John

            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? ;)

            I Offline
            I Offline
            Itanium
            wrote on last edited by
            #5

            Thanx John! 101% what i needed.:) Reagards Farrukh sorry for my bad English.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups