Thread: Counting braces within a txt file

  1. #1
    Registered User
    Join Date
    Nov 2011

    Counting braces within a txt file

    This is my first experience in I'm still learning. Please forgive any syntax or language errors. I am trying to write a program that reads from a .txt and verify the proper pairing of braces. For each left brace encountered there must be a right brace to close the statement. If rightCount ever exceeds leftCount, inserts the characters "??" at that point in the output. Here is what I have. Please any suggestions would be useful. It may not be a kosher program...but then again, that's why I'm here.

    int main(void)
    FILE *fpFileInput;
    char ch;
    int count = 0;
    int getc (FILE *fpFileInput);
    int leftCount = 0;
    int rightCount = 0;
    /* Verify File Access */
    if ((fpFileInput = fopen("text.txt", "r")) == NULL)
    printf ("\ERROR opening input file.\n");
    while (ch != EOF)
    ch = getc (fpFileInput);
    if (ch == '{') leftCount++;
    if (ch == '}') rightCount++;
    /* Test BracesCount */
    if (rightCount > leftCount) printf ("??");
    elseif (leftCount > rightCount) printf ("??");
    printf ("%c", ch);
    } /* End While */
    /* Close the Inupt File */
    fclose (fpFileInput);
    /* Test and Print Results */
    /* NOT DONE HERE*/
    if (leftCount > rightCount)
    printf ("ERROR: Missing %d right braces.\n",rightCount );
    elseif (rightCount > leftCount)
    printf ("ERROR: Missing %d left braces.\n",leftCount );

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    1) You shouldn't post pre-coloured text into code tags. The forum software does it for you.

    2) learn how to indent your code in some meaningful way ... Click HERE

    3) Your means of tracking unmatched braces is flawed because leftcount will routinely exceed right count every time a block is opened.

    4) main() promises to return an integer to the OS... you do not. Here's the minimum C program...
    int main (void)
         // your code here
        return 0; }
    And before you protest... yes it does matter. Your program may work correctly but if it's not interfacing with the OS correctly it can cause other kinds of errors.

    You might investigate a somewhat simpler form of that, tracking only open braces...
    a) Each time you find an open brace, increment a counter...
    b) Each time you find a closing brace, decrement the same counter...
    c) if at the end of the file the counter is not 0... there's a mismatch.

  3. #3
    Join Date
    Dec 2007
    Why is this in here?

    int getc (FILE *fpFileInput);
    There should already be a function by that name and signature in stdio.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with 'character counting from a .txt file'
    By master_vst in forum C Programming
    Replies: 5
    Last Post: 11-09-2008, 02:17 PM
  2. Counting words in a file
    By goran00 in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 04:56 PM
  3. Counting Int's in a File
    By Djanvk in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2006, 02:10 PM
  4. counting certain elements from a file
    By greenstock in forum C++ Programming
    Replies: 17
    Last Post: 01-01-2006, 08:37 PM
  5. Counting records in a file
    By mjpars in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 06:53 AM