Know the line number of a txt file in c

2

Is there any function or way to find out how many lines a file has, without having to open it? I need to split a file into two parts so that two processes read it at the same time. I will use the fseek () function to move the pointer to a certain position of the file, but I need to find out how many lines the file has to be split.

    
asked by anonymous 21.05.2014 / 00:57

1 answer

5

Without opening the file I believe you can not get this information.

But if you use UNIX you can use the command wc to find the number of lines.

wc nomedoarquivo

If you decide to open the file, a way I always use to scroll through the file quickly and count the number of lines is with this code:

while (EOF != (scanf("%*[^\n]") && scanf("%*c"))) 
    ++numLinha;
    
21.05.2014 / 01:19