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
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
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();
}
}
By the way why is the title serialization? what has that got to do with writing to a file?
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
good job, necroposting like a boss from 2 years ago.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.