Difference between revisions of "ReadFile - AS 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "==The Code== <syntaxhighlight lang=Csharp> </syntaxhighlight> ==Explanation==")
 
 
Line 2: Line 2:
  
 
<syntaxhighlight lang=Csharp>
 
<syntaxhighlight lang=Csharp>
 
+
static void ReadFile(char[,] Field)
 +
{
 +
    string FileName = "";
 +
    string FieldRow = "";
 +
    Console.Write("Enter file name: ");
 +
    FileName = Console.ReadLine();
 +
    try
 +
    {
 +
        StreamReader CurrentFile = new StreamReader(FileName);
 +
        for (int Row = 0; Row < FIELDLENGTH; Row++)
 +
        {
 +
            FieldRow = CurrentFile.ReadLine();
 +
            for (int Column = 0; Column < FIELDWIDTH; Column++)
 +
            {
 +
                Field[Row, Column] = FieldRow[Column];
 +
            }
 +
        }
 +
        CurrentFile.Close();
 +
    }
 +
    catch (Exception)
 +
    {
 +
        CreateNewField(Field);
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==Explanation==
 
==Explanation==

Latest revision as of 09:26, 3 March 2017

The Code

static void ReadFile(char[,] Field)
{
    string FileName = "";
    string FieldRow = "";
    Console.Write("Enter file name: ");
    FileName = Console.ReadLine();
    try
    {
        StreamReader CurrentFile = new StreamReader(FileName);
        for (int Row = 0; Row < FIELDLENGTH; Row++)
        {
            FieldRow = CurrentFile.ReadLine();
            for (int Column = 0; Column < FIELDWIDTH; Column++)
            {
                Field[Row, Column] = FieldRow[Column];
            }
        }
        CurrentFile.Close();
    }
    catch (Exception)
    {
        CreateNewField(Field);
    }
}

Explanation