Would you like to react to this message? Create an account in a few clicks or log in to continue.

    File Input & Output

    avatar
    Admin
    Admin


    Posts : 77
    Join date : 2009-07-28

    File Input & Output Empty File Input & Output

    Post  Admin Tue Jul 28, 2009 5:58 am

    Directories
    In order to handle Directories, we have to first import the
    System.IO et System.Text libraries.

    Exists
    A Boolean that returns TRUE if the Folder/File Specified in the path string is found , else FALSE.
    Code:
    string path_str = @"C:\Sleeper\";
    if(!Directory.Exists(path_str))
    ...
    string path_str = path_str +"config.cfg";
    if(!File.Exists(path_str))
    ...
    avatar
    Admin
    Admin


    Posts : 77
    Join date : 2009-07-28

    File Input & Output Empty Re: File Input & Output

    Post  Admin Wed Jul 29, 2009 5:27 am

    System.IO.StreamReader
    Used to explore streams that aren't files.

    Constructor(path)
    Constructs a Read line stream from the file in the specified path (default UTF Cool.

    Fields
    bool EndOfStream , returns true if the stream was read entirely.

    Methods

    Close
    Closes the stream and liberates the resources allocated for the stream, MUST DO after finishing.

    Peek
    Returns the next character in the stream without consuming a Read

    Read
    Consumes one character. //Conversion from int to char needed.

    Read(CharTab, index,length)
    Reads length charrs from index and stores them in CharTab // Conversion needed (char)

    ReadLine
    Returns the next line in the stream or null if it's it's end.

    ReadToEnd



    Code:
    1. using System;
    2. using System.IO;
    3.
    4. namespace Chap3 {
    5. class Program {
    6. static void Main(string[] args) {
    7. // répertoire d'exécution
    8. Console.WriteLine("Répertoire d'exécution : "+Environment.CurrentDirectory);
    9. string ligne = null;
    10. StreamReader fluxInfos = null;
    11. // lecture contenu du fichier infos.txt
    12. try {
    13. // lecture 1
    14. Console.WriteLine("Lecture 1----------------");
    15. using (fluxInfos = new StreamReader("infos.txt")) {
    16. ligne = fluxInfos.ReadLine();
    17. while (ligne != null) {
    18. Console.WriteLine(ligne);
    19. ligne = fluxInfos.ReadLine();
    20. }
    21. }
    22. // lecture 2
    23. Console.WriteLine("Lecture 2----------------");
    24. using (fluxInfos = new StreamReader("infos.txt")) {
    25. Console.WriteLine(fluxInfos.ReadToEnd());
    26. }
    27. } catch (Exception e) {
    28. Console.WriteLine("L'erreur suivante s'est produite : " + e.Message);
    29. }
    30. }
    31. }
    32. }
    avatar
    Admin
    Admin


    Posts : 77
    Join date : 2009-07-28

    File Input & Output Empty Re: File Input & Output

    Post  Admin Wed Jul 29, 2009 2:56 pm

    System.IO.StreamWriter

    Constructor(pathToFile)

    Fields
    virtual boolean : AutoFlush
    Writing mode in the file : False = the writing n the stream is not imediate, there is first writing on a temporary memory then on the file.
    Use TRUE ON network.

    METHODS

    Close
    Must be used or file will be unusable.

    Flush
    Quick write ?

    WriteLine
    Self Defined.

    Sponsored content


    File Input & Output Empty Re: File Input & Output

    Post  Sponsored content

      Similar topics

      -

      Current date/time is Mon May 20, 2024 8:23 am