我是 C 的新手,正在尝试编写一个计算括号、大括号、空格和换行符的程序。编译器发出错误:Error opening fileSegmentation fault (core dumped)
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char c;
int blank, tab, openbrace, closedbrace,openbracket,closedbracket, newline=0;
if ((fp = fopen("argv[0]", "r")) == NULL)
{
fprintf(stderr, "Error opening file");
}
while ((c=fgetc(fp)) != EOF)
{
switch(c)
{
case '{':
openbrace++;
case '}':
closedbrace++;
case '[':
openbracket++;
case ']':
closedbracket++;
case '\n':
newline++;
}
}
fclose(fp);
printf("\nThe number of { are %d", openbrace);
printf("\nThe number of } are %d", closedbrace);
printf("\nThe number of [ are %d", openbracket);
printf("\nThe number of ] are %d", closedbracket);
printf("\nThe number of new lines are %d", newline);
}