[C#] écriture fichier binaire

scoubidou944 Messages postés 714 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 19 janvier 2017 - 12 avril 2004 à 21:50
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 13 avril 2004 à 08:34
Dans la série il va falloir que je pense moins bas-niveau... chapitre écriture dans un fichier binaire.

j'ai une classe qui contient des int, int[], char & string.
Maintenant faut que je pose tout cela dans un fichier binaire.

Alors en C, fwrite() CT quand meme bien pratique et la je cherche la différence. G bien trouvé l'exemple à la fin mais ca me plait pas de voir le nom de mes champs dans le fichier final parce que ca prend plus de place et que ca donne des informations sur l'utilisation de la donnée.

Exist-il une alternative ?

----------------------------
C++ forever
C# amateur

// ---------------------------
// SOURCE
// ---------------------------
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace MyObjSerial
{
[Serializable()] //Set this attribute to all the classes that you define to be serialized
public class Employee : ISerializable
{
public int EmpId;
public string EmpName;

//Default constructor
public Employee()
{
EmpId = 0;
EmpName = null;
}

//Deserialization constructor.
public Employee(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
EmpId = (int)info.GetValue("EmployeeId", typeof(int));
EmpName = (String)info.GetValue("EmployeeName", typeof(string));
}

//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
// then you should read the same with "EmployeeId"
info.AddValue("EmployeeId", EmpId);
info.AddValue("EmployeeName", EmpName);
}
}

//Main class
public class ObjSerial
{
public static void Main(String[] args)
{
//Create a new Employee object
Employee mp = new Employee();
mp.EmpId = 10;
mp.EmpName = "Omkumar";

// Open a file and serialize the object into it in binary format.
// EmployeeInfo.osl is the file that we are creating.
// Note:- you can give any extension you want for your file
// If you use custom extensions, then the user will now
// that the file is associated with your program.
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

Console.WriteLine("Writing Employee Information");
bformatter.Serialize(stream, mp);
stream.Close();

//Clear mp for further usage.
mp = null;

//Open the file written above and read values from it.
stream = File.Open("EmployeeInfo.osl", FileMode.Open);
bformatter = new BinaryFormatter();

Console.WriteLine("Reading Employee Information");
mp = (Employee)bformatter.Deserialize(stream);
stream.Close();

Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString());
Console.WriteLine("Employee Name: {0}",mp.EmpName);
}
}
}

2 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 avril 2004 à 08:33
ah ba là c'est de la serialisation
avec le fwrite c'est toi qui controlait l'ecriture dans le fichier
pour l'instant je n'ai eu besoin que du "mode texte" mais il y a BinaryWriter, a tester

Cocoricoooooooo !!!!
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 avril 2004 à 08:34
et petit rappel : y'a une balise pour mettre du code (avant dernier bouton de la barre d'outils en dessous de la zonne de saisie), c'est quand même plus sympa pour celui qui doit lire le code :)

Cocoricoooooooo !!!!
0
Rejoignez-nous