2
Is there a function or way to find out how many lines a file has without opening it? I need to divide 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.
There is no universal way to find out how many lines a file has without opening it. In Unix and its derivations, you can use the stat or fstat command in C to find out what size in bytes the file is. With this, you can open the file in two processes, dividing by the size in bytes of the file. Even so, the recommended way to find out how many lines or how many bytes a file has is to open it and count the bytes or new line characters. You can use the fseek function for this.
– Bernardo Botelho
Are the lines in your file all the same length? If you know how many bytes the file occupies (stat()) and divide by line size get the number of lines without opening the file.
– pmg