[c#] tutorial basique (string, stream, fichier texte, fichier binaire)

Contenu du snippet

pas grand chose à dire si ce n'est que C un premier tutorial qui rassemble les bases du C# mais qui demande de connaitre la syntaxe un minimum :
- Gestion des strings
- Gestion de la console
- Gestion des execeptions
- Lecture/écriture dans un fichier texte
- Lecture/écriture dans un fichier binaire

Bonne base sinon pour commencer :
http://www.codeproject.com/csharp/quickcsharp.asp

N'hésitez pas à mettre des tutoriaux de 100 ou 200 lignes qui regroupent une catégory de dev' ca permet à ceux qui débutent de pas arpenter le net à la recherche d'info sur un même sujet ;p

Source / Exemple :


using System;
using System.IO;
using System.Drawing;

namespace T00_Core_StringStream
{
	/// <summary>
	/// Summary description for T00_Core_StringStream
	/// </summary>
	class T00_Core_StringStream
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			/*
			// Variable initialization

  • /
int iMyInteger = 25; string szString1 = " My First String "; string szString2 = szString1 + ' ' + "Another String."; int[] aiMyIntArray = new int [iMyInteger]; Color oMyColor = Color.Blue; for (int i =0; i < aiMyIntArray.Length; i++) { aiMyIntArray [i] = (1 << i); } /* // Variable modification - Basics
  • /
// Suppress space & tabs at beginning & end of string & copy result in szString3 string szString3 = szString1.Trim (); // Get offset in szString3 where the word 'First' starts int iIndexOfWord = szString3.IndexOf ("First"); // Copy in szString4 from last word start position to end string szString4 = szString3.Substring (iIndexOfWord, szString3.Length - iIndexOfWord); // Format a string & write it to console string szString5 = String.Format ("This is equivalent to sprintf() in C with various arguments:\n- {0}\n- {1}\n- {2}", iMyInteger, szString3, szString4); Console.WriteLine (szString5); /* // Console manage
  • /
string szInput = ""; int iTest = 0; float f32Test = 0; // No exception during read operation Console.WriteLine("\nType in an integer (No Exception) :"); szInput = Console.ReadLine(); iTest = int.Parse(szInput); Console.WriteLine("Type in a decimal (format like 1,5) (No Exception) :"); szInput = Console.ReadLine(); f32Test = float.Parse(szInput); Console.WriteLine("int: {0} float: {1}", iTest, f32Test); // Use exceptions try { Console.WriteLine("\nType in an integer (WITH Exceptions) :"); szInput = Console.ReadLine(); iTest = int.Parse(szInput); } catch (FormatException e) { Console.WriteLine("Integer : Invalid input recieved, using default value of 7 (Exception {0})", e.ToString ()); iTest = 7; } try { Console.WriteLine("Type in a decimal (format like 1,5) (WITH Exceptions) :"); szInput = Console.ReadLine(); f32Test = float.Parse(szInput); } catch (FormatException e) { Console.WriteLine("Decimal : Invalid input recieved, using default value of 1.5 (Exception {0})", e.ToString ()); f32Test = 1.5f; } Console.WriteLine("int: {0} float: {1}", iTest, f32Test); /* // Text file manage
  • /
// Write in a text file StreamWriter oWriteTextStream = new StreamWriter ("MyFile.txt"); oWriteTextStream.WriteLine ("This is a text file"); oWriteTextStream.WriteLine (szString5); oWriteTextStream.Close (); // Read from text file StreamReader oReadTextStream = new StreamReader ("MyFile.txt"); string szStringResult; int iCounter = 0; while ((szStringResult = oReadTextStream.ReadLine ()) != null) { Console.WriteLine("Read line {0} {1}", iCounter, szStringResult); iCounter++; } oReadTextStream.Close (); /* // Binary file manage
  • /
// Write in binary file Stream oWriteBinStream = File.Open("MyFile.bin", FileMode.Create); BinaryWriter oWriteBinOutput = new BinaryWriter (oWriteBinStream); oWriteBinOutput.Write (iMyInteger); oWriteBinOutput.Write (szString1); oWriteBinOutput.Write (oMyColor.ToArgb ()); // Convert color to integer oWriteBinOutput.Close (); oWriteBinStream.Close (); // Read in binary file Stream oReadBinStream = File.Open("MyFile.bin", FileMode.Open); BinaryReader oReadBinOutput = new BinaryReader (oReadBinStream); iMyInteger = oReadBinOutput.ReadInt32 (); szString1 = oReadBinOutput.ReadString(); iMyInteger = oReadBinOutput.ReadInt32 (); oMyColor = Color.FromArgb (iMyInteger); // Convert integer to color oReadBinOutput.Close (); oReadBinStream.Close (); } } }

Conclusion :


Just have fun ;p

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.