How to use fgets to read a file line by line

前端 未结 3 1367
庸人自扰
庸人自扰 2021-01-25 04:21

I\'m new at programming so there are some basics and maybe common sense that I don\'t know. I have a question about how to use fgets right. Based on the explanation of fgets, it

3条回答
  •  爱一瞬间的悲伤
    2021-01-25 05:05

    fp = fopen("sample.txt", "r");
    while (1) {
            if (fgets(line,150, fp) == NULL) break;
            i++;
            printf("%3d: %s", i, line);
    }
    printf("%d\n",i);
    

提交回复
热议问题