C语言 读取文本文件中的内容跳过#开头的注释后读取数据,把功能写成主函数能实现,作为功能函数在主函数里面调用就会出现Segmentation fault (core dumped)
函数如下:
void readPortInfo(const char* port_info)
{
FILE* fp = fopen(port_info, "r");
char* p = NULL;
fscanf(fp, "%c", p);
printf("test1 == %c\n", p);
while (strcmp(p, "#") == 0 || strcmp(p, "\n") ==0)
{
fscanf(fp, "%[^\n]\n");
fscanf(fp, "%c", p);
printf("test2 == %c\n", p);
}
fscanf(fp, "%[^\n]\n");
int a,b,c,d,e,f,g,h;
while (fscanf(fp, "%d %d %d %d %d %d %d %d",
&a, &b, &c, &d, &e, &f, &g, &h) != EOF)
{
printf("%d %d %d %d %d %d %d %d\n", a,b,c,d,e,f,g,h);
}
fclose(fp);
return;
}
int main()
{
readPortInfo("port_info");
return 0;
}