Count lines on text file

Hello, I want make a line count program in C. For example:

I have a text file with content:

hello
my
name
is
John


this file have 5 lines. Ok, I need count lines in this file using C. My code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>

int main()
{
    FILE* fp;
    int countLines = 0;
    int i;

    fp = fopen("file.txt", "r");
    if (fp == NULL)
    {
        printf("We can't open the file.");
        fclose(fp);
        return 1;
    }
    else
    {
        while((i=fgetc(fp))!=EOF)
        {
            //...
        }

    }

}


This line:

while((i=fgetc(fp))!=EOF)

is right? What I count line numbers of file?

Thanks.

Last edited on
I'm not shure what fgetch () does but try insert if statement into while statement:

1
2
short counter;  //declare it out of while scope
if (fgetch (fp) == '/0' || '/n') ++coutner; //insert into while statement 
I'm not shure what fgetch () does but try insert if statement into while statement:

short counter; //declare it out of while scope
if (fgetch (fp) == '/0' || '/n') ++coutner; //insert into while statement


Thanks. But, wheres the EOF (end of file)? o.O
it's inside while ^^

1
2
3
4
5
       while((i=fgetc(fp))!=EOF)
        {
               if (fgetch (fp) == '/0' || '/n') ++coutner;
               //other code
        }
Last edited on
Thanks again xD

So. My code is this now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>

int main()
{
    FILE* fp;
    int countLines = 0;
    int i;


    fp = fopen("arquivo.txt", "r");
    if (fp == NULL)
    {
        printf("We can't open the file.");
        fclose(fp);
        return 1;
    }
    else
    {
        while((i=fgetc(fp))!=EOF)
        {
            if (getch (fp) == '/0' || '/n')
                ++i;
        }

        printf("Number of lines: %d\n",i);


    }

}


but, it ever return -1 :-(

What's wrong? =O

thanks.
Last edited on
I do not know if it is your code true
in c++ can your program write like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
    char a;
    int b=0;
    fstream file;
    file.open ("countlines.txt");
    file.seekg (0);
    while (file.good ())
    {
        a=file.get ();
        if (a=='\n')
        {
            b++;
        }
    }
    cout << "In file is "<< b<<" lines"<< endl;
    cout << "\n";
    system ("pause");
}

I hope it will help you
program is very unflexible but it works (counting lines in countlies.txt)
btw sory for my english I hope you understood what I wished said
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>

int main() {

    FILE* fp;
    int i;

    fp = fopen("arquivo.txt", "r");
    if (fp == NULL)
    {
        printf("We can't open the file.");
        fclose(fp);
        return 1;
    }
        while((fgetc(fp) != EOF) { // while EOF don't count
            if (getch (fp) == '/0' || '/n') ++i;  //count here only
          }

        printf("Number of lines: %d\n",i);

return 0;  // u are missing this

}
Last edited on
Thaaaaaaaaaaaaaaaaaaanks! Solved! The code working is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>

int main()
{
    FILE* fp;
    int countLines = 1;
    int i;


    fp = fopen("arquivo.txt", "r");
    if (fp == NULL)
    {
        printf("We can't open the file.");
        fclose(fp);
        return 1;
    }
    else
    {
        while((i=fgetc(fp))!=EOF)
        {
            if (i == '\n')
                countLines++;
        }

        printf("Number of lines: %d\n",countLines);
    }
        return 0;
}


Thanx for all :-)
Topic archived. No new replies allowed.