Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram for checking file content. Is there a better way?
    primarykey
    data
    text
    <p>I've created a very basic 'debugging' program that checks if a c source file has the same number of opening and closing curly brackets, square brackets and parentheses. I have a code that's fairly simple and it works but the code seems unnecessarily long. I was considering using arrays instead. An array to store each {,[,( and another to store },],) then counting the instance of each and comparing the amounts. But I think that code would be almost as long. What do you guys think?</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; int main(void) { FILE *fp; char fname[20]; char c; int curlybracket = 0; int curlybracketr = 0; int squarebracket = 0; int squarebracketr = 0; int parentheses = 0; int parenthesesr = 0; printf("Please enter the destination of the file: \n"); scanf("%s", fname); fp = fopen(fname, "r"); if (fp == NULL) { printf("Problem opening file!\n"); exit(0); } else { printf("File opened correctly\n"); } while (c != EOF) { c = getc(fp); if (c == '{') { curlybracket++; } if (c == '[') { squarebracket++; } if (c == '(') { parentheses++; } if (c == '}') { curlybracketr++; } if (c == ']') { squarebracketr++; } if (c == ')') { parenthesesr++; } } if (curlybracket == curlybracketr) { printf("There are an equal number of curlybrackets\n"); } else { printf("There is an unequal number of curlybrackets\n"); return 0; } if (squarebracket == squarebracketr) { printf("There are an equal number of squarebrackets\n"); } else { printf("There are an unequal number of squarebrackets\n"); } if (parentheses == parenthesesr) { printf("There are an equal number of parentheses\n"); } else { printf("There are an unequal number of parentheses\n"); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload