与其尝试使用 fscanf,不如使用带有 ';' 的“getdelim” 作为分隔符。根据手册页
" getdelim() 的工作方式与 getline() 类似,不同之处在于可以将换行符以外的行分隔符指定为分隔符参数。与 getline() 一样,如果在文件结尾之前的输入中不存在分隔符,则不会添加分隔符达到了。”
所以你可以做类似(未经测试和未编译的代码)
char *line = NULL;
size_t n, read;
int alloc = 100;
int lc = 0;
char ** buff = calloc(alloc, sizeof(char *)); // since you don't know the file size have 100 buffer and realloc if you need more
FILE *fp = fopen("FILE TO BE READ ", "r");
int deli = (int)';';
while ((read = getline(&line, &n, fp)) != -1) {
printf("%s", line); // This should have "input A0;"
// you can use either sscanf or strtok here and get A0 out
char *out = null ;
sscanf(line, "input %s;", &out);
if (lc > alloc) {
alloc = alloc + 50;
buff = (char **) realloc(buff, sizeof(char *) * alloc);
}
buff[lc++] = out
}
int i = 0 ;
for (i = 0 ; i < lc; i++)
printf ("%s\n", buff[i]);