1,105,578 Community Members

serialization

Member Avatar
ayesha25
Newbie Poster
7 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i have a class like

class abc{
struct {
string name;
float [] marks;
}
....
.
.
.
.
.
}

after implementing my all logic i want to save marks array on to a file,,,,,how can i achieve this,,,,please need urgent help

Member Avatar
requimrar
Newbie Poster
20 posts since Apr 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I am currently booted into ubuntu so I can't check. If i'm not wrong, it goes something like this:

using System.IO //Important

class abc
{    
    FileStream file = new FileStream("path.txt", FileMode.Create, FileAccess.ReadWrite); // replace path.txt with the file to write to

    struct marks
    {
        string name;
        float[] marks = new float[10];
        //etc
    }

    public void writetofile (float[] marks)
    {
        StreamWriter sw = new StreamWriter(file)

        for(int i = 0; i < marks.Length; i++)
        {
            sw.WriteLine(marks[i].ToString);
        }
        sw.Close();
    }

}
Member Avatar
requimrar
Newbie Poster
20 posts since Apr 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

By the way why is the title serialization? what has that got to do with writing to a file?

Member Avatar
prit005
Light Poster
39 posts since Jul 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DemoSerialization
{
    [Serializable]
    class SerializationDemo:IPatient
    {
       private int _age;
       private string _name;
       private static string _localFileName = string.Empty;

       public static string LocalFileName
       {
           get
           {
               return SerializationDemo._localFileName = "C:\\ImageSerializeLog.txt";
           }
           set
           {
               SerializationDemo._localFileName = value;
           }
       }
       #region IPatient Members

     public int Age
       {
           get
           {
               return _age;
           }
           set
           {
               _age = value;
           }
       }

      public string Name
       {
           get
           {
               return _name;
           }
           set
           {
               _name = value;
           }
       }

       #endregion

        static void Main(string[] args)
        {
            SerializationDemo objSerializationDemo = new SerializationDemo();           
            objSerializationDemo.Age = 25;            
            objSerializationDemo.Name = "XYZ";
            IFormatter objBinaryFormatter = new BinaryFormatter();
            if (File.Exists(LocalFileName))
            {
                File.Delete(LocalFileName);
                //Serialization.
                Stream objSerializeStram = new FileStream(LocalFileName, 
                               FileMode.Create, 
                               FileAccess.ReadWrite,
                               FileShare.None);
                objBinaryFormatter.Serialize(objSerializeStram, objSerializationDemo);
                objSerializeStram.Close();

                // Deserialization.
                Stream objDeSerializeStram = new FileStream(LocalFileName, 
                               FileMode.Open,
                               FileAccess.Read,
                               FileShare.Read);
                objSerializationDemo =(SerializationDemo)objBinaryFormatter.Deserialize(objDeSerializeStram);
                objDeSerializeStram.Close();
                Console.WriteLine("Age:-{0} and Name:-{1}",objSerializationDemo.Age,objSerializationDemo.Name);
                 Console.ReadLine();
            }
        }


    }
}

also one of the example for serialization

Member Avatar
requimrar
Newbie Poster
20 posts since Apr 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

good job, necroposting like a boss from 2 years ago.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article