计数使用C线的数量 [英] Count number of line using C

查看:91
本文介绍了计数使用C线的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用C来算在我的文件中的行数?

Is there a way to count the number of lines in my file using C?

推荐答案

如果您希望以编程方式执行此,打开在文本模式下的文件,直到到达文件结尾执行龟etc()操作。保持的时间龟etc被称为数的计数

If you want to perform this programmatically, open the file in text mode and perform fgetc() operation until you reach end of file. Keep a count of number of times fgetc was called.

    FILE *fp = fopen("myfile.txt");
    int ch;
    int count=0;
    do
    {
        ch = fgetc(fp);
        if(ch == '\n') count++;   
    } while( ch != EOF );    

    printf("Total number of lines %d\n",count);

这篇关于计数使用C线的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆