How to Count Lines of a File in C++

C function that counts lines in file

while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}

But please note: Why is “while ( !feof (file) )” always wrong?.

Counting the number of lines in a .txt file in c

Apart from the char that should be an int your code is more or less fine. The problem is somewhere in the code you didn't show.

This works:

#include <stdio.h>

int main() {
FILE* fp = fopen("processes.txt", "r");
if (fp == NULL)
{
printf("Could not open file.");
return 1;
}

int c; // this must be an int
int count = 0;

for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;

printf("The file has %d line(s)\n", count);

fclose(fp);
}

However if the last line of the file does not end with a \n, it is not counted.

How to count number of lines in a file in C?

ftell reports the current position in the file. For a file that has just been opened, the position is the start, and ftell returns zero. Then, since size is zero, the loop in findnum_lines processes zero characters and reports no lines were found.

Generally, you do not want to get the file size and then loop on that. One reason is that the file can change while you read it—other processes may write more data to it or may truncate it. Another reason is that it is unnecessary. Just read characters until you get EOF, using a while loop (or a do … while loop).

Additionally, char line; should be int line; because it is used to hold the result of getc, which may be either a character or EOF, and a char is inadequate to hold EOF. (Also, getc returns the character’s value as an unsigned char converted to an int, so, in an implementation where char is signed, char cannot even properly represent all characters.)

How to count lines of a file in C++?

How about this :-

  std::ifstream inFile("file"); 
std::count(std::istreambuf_iterator<char>(inFile),
std::istreambuf_iterator<char>(), '\n');

Counting number of lines in the file in C

fgets() reads till newline character or till the buffer is full

char buf[200];
while(fgets(buf,sizeof(buf),fileHandle) != NULL)
{
count++;
}

fgetc() is an issue here because you encounter EOF first and exit your do while loop and never encounter a \n character so count remains untouched for the last line in your file.If it happens to be there is a single line in your file that the count will be 0

Count lines of a file using file descriptor in C

You're not allocating any memory for miniBuffer which is an array of char pointers. Which isn't really the problem - the problem is that it shouldn't be an array of char pointers in the first place. You only need it to be an array of char like the following.

char miniBuffer[1];

And the other change then is to check that single element of the array for it being a \n character.

if (miniBuffer[0] == '\n')

You might find it would be more efficient to read in larger chunks by increasing the size of the array and use functions like strchr to find any \n in the string. You would need to store the amount read returns so you could properly NUL terminate the string though.

c count lines in a file avoid EOF

fgetc() returns the character read as an unsigned char cast to an int or EOF. Hence declaring chr as int instead of char should solve the issue.

Count number of line using C

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);


Related Topics



Leave a reply



Submit