CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 20

Threaded View

  1. #1
    Join Date
    Jun 2010
    Posts
    23

    Need help with bracket counter for project

    Hey guys!

    So so far what my program does is goes through a file that you specify and takes the lines of text between 2 keywords and saves them to a seperate file for each block of text.

    i.e

    Start
    stuff
    stuffer
    End

    Start1
    stuff
    stuffer
    End

    The "stuff" in between will be saved to a diff text file for each block
    But what i am trying to do now is instead of using keywords to mark the beginning and end i want to use brackets to mark it.

    Start
    [
    [
    stuffer
    ]
    ]
    End

    So the program instead of using Start and End to mark the beginning and end could use the bracket count....wouldnt it be something like:

    if(line.equals("["))
    counter = counter + 1
    if(line.equals("]"))
    counter = counter - 1

    so that when counter reaches 0 it will know that it has reached the end of a block of code...

    this is what I have so far and the file that I have been using for testing

    private void button1_Click(object sender, EventArgs e)
    {
    string path = "c:\\";
    int counter = 0;
    var masterList = new List<List<string>>();
    using (var sr = new StreamReader(path + "test.txt"))
    {
    while (!sr.EndOfStream)
    {
    string line = sr.ReadLine();


    if (line.StartsWith("CLASS") || line.StartsWith("TYPE"))
    {
    if (line.Equals("{"))
    {
    counter = counter + 1;
    }

    if (line.Equals("}"))
    {
    counter = counter - 1;
    }

    var lineList = new List<string>();
    while (!line.Equals("END"))
    {
    lineList.Add(line);
    line = sr.ReadLine();
    }
    masterList.Add(lineList);
    }
    }
    }

    foreach (var lineList in masterList)
    {
    using (var sw = new StreamWriter(path + lineList[0] + "_test.txt"))
    {
    foreach (string line in lineList)
    sw.WriteLine(line);

    textBox1.Text = Convert.ToString(masterList.Count);

    }


    }


    }
    }
    }
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured