Im working on this analyzer program that reads input from a file and breaks characters into various tables.

Ive got the output pretty much done, except there's an issue with some of it, and Im pretty sure its due to the input problem.

so heres what the input looks like

int fact( int x )
/* recursive factorial function */
{ if (x > 1)
    return x * fact(x-1);
  else
    return 1;
}

void main( void )
{ int x;
  x = read();
  if (x > 0) write( fact(x) );
}

and Im supposed to echo (make a copy of the input to the output file...with one thing that shouldnt be there: the commented line / recursive factorial function /)

so in essence, it should look like this:

int fact( int x )
{ if (x > 1)
    return x * fact(x-1);
  else
    return 1;
}

void main( void )
{ int x;
  x = read();
  if (x > 0) write( fact(x) );
}

The problem Im having is there is no ignore function in C# like in C++, so I dont know how to make the program ignore this comment line.
Additionally, the last bracket "}" of the void main function is not displaying.
its truncated somehow. so all i get is:

int fact( int x )
/* recursive factorial function */
{ if (x > 1)
    return x * fact(x-1);
  else
    return 1;
}

void main( void )
{ int x;
  x = read();
  if (x > 0) write( fact(x) );

here is my code that reads in the input file, and outputs this echo/copy of the input:

internal static class Define_input_output
{
    public const string inFile = "input.txt"; //set file to read
    public const string outFile = "output.txt"; //set file to write to
}

    public static void read_inputFile(StreamReader input, StreamWriter output)
{
    char character;
    string word = "";

    character = (char)input.Read();

    while (!input.EndOfStream)
    {
        if (character == '\n')
        {
            output.Write(character);
        }
        //else if (character == '/' && input.Peek() == '*') //if character is / and next character is *, this line is a comment
        //{
        //    ; //ignore all characters until end of line
        // THIS IS WHERE AN IGNORE FUNCTION SHOULD BE
        //}
        else if (character == ' ') 
        {
            output.Write(character); 
        }
        else 
        {
            output.Write(character); 
        }
        character = (char)input.Read(); //read in next character
    }
}

public static void neglectComment(StreamReader input)
{
    bool isComment = false; //initialize isComment to false

    while (!isComment) //loop until comment has been removed from stream
    {
        isComment = true; //comment has been taken out
    }
}

static int Main()
{
    StreamReader input = new StreamReader(Define_input_output.inFile);//input file
    StreamWriter output = new StreamWriter(Define_input_output.outFile);//output file

    while (!input.EndOfStream) //read each line until end of file
    {
        read_inputFile(input, output); 
    }

    input.Close(); //close input file
    output.Close(); //close output file

    return 0;
}

Thanks in advance

Edited 2 Months Ago by Reverend Jim: Moved to programming forum

Well, for starters the "/ ... /" comment supports multi-line, so you'll need to consider that in your parsing. A simple approach would be a loop that reads and throws away characters until the closing token is found:

    public static void processInput(StreamReader input, StreamWriter output)
    {
        int ch;

        while (!input.EndOfStream)
        {
            ch = input.Read();

            if (ch == '/' && input.Peek() == '*')
            {
                neglectComment(input);
            }
            else
            {
                output.Write((char)ch);
            }
        }
    }

    public static void neglectComment(StreamReader input)
    {
        input.Read(); // Extract the peeked asterisk

        while (!input.EndOfStream && input.Read() != '*' && input.Peek() != '/')
            ;

        input.Read(); // Extract the peeked slash
    }

    static void Main()
    {
        using (var input = new StreamReader(Define_input_output.inFile))
        {
            using (var output = new StreamWriter(Define_input_output.outFile))
            {
                processInput(input, output);
            }
        }
    }

Though this somewhat depends on your future plans for this application. It might be better to perform a proper parsing of the code, though that's quite a bit more work.

Something else to consider, there is another type of comments and both types of comments can be appended to a line of code. Here's some code that will ignore both types of comments, using the whole line or appended tot he end of a line:

    //append is true to add code to an existing file.
    private static void StripComments(string inputFilePath, string outputFilePath, bool append)
    {
        using (StreamReader sr = new StreamReader(inputFilePath))
        {
            using (StreamWriter sw = new StreamWriter(outputFilePath, append))
            {
                string temp = "";
                while (!sr.EndOfStream)
                {
                    int currChar = sr.Read();
                    int prevChar = '\n';
                    //Look for beginning comment character
                    if (currChar == '/')
                    {
                        int peek = sr.Peek();
                        //Look for single line comment character
                        if (peek == '/')
                        {
                            while (currChar != '\n')
                            {
                                currChar = sr.Read();
                            }
                            //If the previous character written wasn't a newline
                            //character then the comment was 
                            //appended to a line of code and a newline needs to
                            //be added.
                            if (prevChar != '\n')
                            {
                                sw.Write((char)currChar);
                                prevChar = currChar;
                            }

                        }
                        //Look for block comment character
                        else if (peek == '*')
                        {
                            do
                            {
                                currChar = sr.Read();
                            } while (!(currChar == '*' && sr.Peek() == '/'));
                            temp = sr.ReadLine();
                        }
                        //If the '/' didn't signify a comment it needs to get written.
                        else
                        {
                            sw.Write((char)currChar);
                            prevChar = currChar;
                        }
                    }
                    else
                    {
                        sw.Write((char)currChar);
                        prevChar = currChar;
                    }

                }
            }
        }
    }

Edited 2 Months Ago by tinstaafl