0

Regarding the Nand2Tetris course, I've gotten to project 7 in the book and, being a huge C# noob, I thought I'd try to implement it in this language as practice. Specifically I'm trying to follow the Parser module's specification and I'm already stumped on the first step, which states that the Parser's constructor has:

  • Arguments: Input file/stream
  • Function: Opens the input file/stream and gets ready to parse it

So far, my main program (Program.cs) is:

namespace VM_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program Main");

            FileStream inFileStream = File.Create(@"c:\Users\<USERNAME>\Documents\Nand2Tetris\nand2tetris\projects\07\VM 1\in.vm");

            Parser parser = new Parser(inFileStream);
        }
    }
}

While the Parser module (Parser.cs) is:

namespace VM_1
{
    public class Parser
    {
        public Parser(FileStream inFileStream)
        {
            Console.WriteLine("Parser Constructor");
        }

        static void ParserMain()
        {
            Console.WriteLine("Parser Main");
        }
    }
}

I don't know if this is even the right way to structure a C# program but my googling has lead me to it and Program.cs and Parser.cs seem to be integrating with no issues.

The problem is that in the Parser module I'm trying to take in the filestream as an argument so that I can open it in its constructor. Therefore in Program.cs I use File.Create(), trying to reserve File.Open() for the Parser module. This is because, as specified in the book, in the Parser's constructor I would have to actually open the filestream, that is, I'm not supposed to open it in Program.cs. But every C# example of opening a file requires a string path as its first argument and I already supplied it in Program.cs. So I'm not sure how to implement this first step in the project. Any help is appreciated.

Thanks

Jaime Salazar
  • 349
  • 1
  • 2
  • 11

1 Answers1

0

Take a look at the documentation you can already read/write to the file stream you have as the result of File.Create. If there is a need to open an already created file just use File.Open and put returned FileStream into your Parser

Yura
  • 75
  • 1
  • 8
  • I know, I could also just pass a string to the Parser and open the file there but I'm trying to follow the instructions to a tee. They specify that the Parser's constructor should receive the file/stream as an argument and then open it in that constructor. – Jaime Salazar Apr 08 '20 at 17:45