计算c中.txt文件中的行数

问题描述 投票:0回答:1

我有一个processs.txt文件,其中包含有关传入进程的详细信息,

0 4 96 30
3 2 32 40
5 1 100 20
20 3 4 30

我想找到此文件中的行数。该怎么办?

我尝试过此代码,但始终将行数返回为0

char c;
int count = 0;
// fp is the pile pointer
for (c = getc(fp); c != EOF; c = getc(fp)) 
        if (c == '\n') // Increment count if this character is newline 
            count = count + 1; 
c memory-management
1个回答
0
投票

也许您执行此操作时文件结尾??并且您需要从头开始

int c;                            // c must be int
int count = 0;
// fp is the pile pointer

rewind(fp);                       // back to beginning, clear error

for (c = getc(fp); c != EOF; c = getc(fp)) 
        if (c == '\n') // Increment count if this character is newline 
            count = count + 1; 
© www.soinside.com 2019 - 2024. All rights reserved.