returning # of text lines

spike8
spike8 used Ask the Experts™
on
how do you write a function that takes a pointed for an open text file and than returns the number of lines that are found in the text file???
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
a line is ended with 13 or 13 followed by 10

So you read some from the file and look for each char with value of 13 and count these.
Just to elaborate on what Kimpan has already said, the presence of these characters depends on what system you are using. The 10, or carrige return, following the 13, line feed, is specific to Windows. In UNIX there is simply a line feed character. So counting the 13 as Kimpan has already said will have the added advantage of accomplishing this task irrespective of whether you are running UNIX or Windows.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
In Unix a line ends with 10, in Windows 10 followed by 13
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Sorry, I meant 13 followed by 10
Huh. Ozo, I was just going on what another expert told me when I said that UNIX ended its lines with 13. Interesting, I think I'll check this out for myself.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Mac ends its lines with 13

Commented:
Open the file in text mode and use '\n' as End Of Line.
  FILE* f = fopen(file_name, "rt");

Commented:
cat filename|wc -l
:P

Commented:
and call above command from program function and somehow try and get the value.. :P

Commented:
Hey, as long as we're being terse, drop the "cat" :)

wc -l <filename>

and if you want to extract just the number of lines:

wc <filename>  | cut -c1-8
fridomCEO/Programmer

Commented:
If it has to be in C and it's a text file you simply
run fgets over it and count how often it is called this
will give you the number of lines *if* the fgets buffer is long enough to hold even the longest line in that file

Regards
Friedrich
Commented:
int line_read(FILE *ptr)
{
    int count=0;
    char buff[256];
    while(fgets(buff,256,ptr)!= 0)
      count++;
    return count;
}

this code assumes that the file pointer is in the 1st position of the file .

Commented:
If you're going to be reading a file of unknown type, I would suggest a buffer size larger than 256...  4096 is normally a good bet, and 65536 is generally safe, too :)

Commented:
The problem is, you can never be sure that some file will not overflow your buffer, no matter what size it is.  Therefore, you have to parse the file one character at a time.

For example:
  http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/wc.c.html
Or, with buffering:
  http://minnie.tuhs.org/UnixTree/MiniUnix/usr/source/s2/wc.c.html

Or, check the GNU wc.c in thiz archive:
  http://mirrors.kernel.org/gnu/textutils/textutils-2.1.tar.gz

Commented:
About EOL conventions: as alexo implied, a standard conforming library is supposed to convert whatever the target OS uses for end of line marker to \n when reading a text stream, and convert \n to whatever the target OS uses for end of line marker when writing a text stream.  The C standard draws the text/binary distinction to make that possible for OSs that use multicharacter end of line markers.

Given that, there's no reason to have to read a whole line into a buffer, unless you really need to do something else that needs to see the whole line at once in addition to counting lines;

  count = 0;
  while ((c = fgetc(fp)) != EOF) {
     if (c == '\n')
         count++;
  }

will suffice, and no worries about how long a line is. stdio keeps its own buffer that doesn't worry about line sizes.

Commented:
geezes, isn't this question ended already?!
I geuss not. I think we ventelated it enough, don't you?
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to  shyma_nk

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial