How to count the number of line in a file ?

On Feb 19, 7:18 am, bedo...@gmail.com wrote:
Thank you for your answer.
Indeed, i did a loop to count line by line.

I thought a more efficient way existed, like  "wc -l" in unix !!!

but it's not the case ...

Thanks and Regards,

b.
Hi,
You can use popen. There is an example in C but it's very simple to
translate it to Skill.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char buf[128];
FILE* f = _popen("wc -l test.txt", "rt");
if(f)
{
fgets(buf, 128, f);
printf("%s\n", buf);
_pclose(f);
}
return 0;
}

The result will be in the first output line like this '4 test.txt'.
There were 4 lines in test.txt.
Regards,
 
Thank you for your answer.
Indeed, i did a loop to count line by line.

I thought a more efficient way existed, like "wc -l" in unix !!!

but it's not the case ...

Thanks and Regards,

b.
 
Andy wrote, on 02/18/08 13:50:
You may try this
lineCnt = 0
inPort = infile("fileName")
while(gets(line inPort)!=nil
lineCnt = lineCnt+1
tokens = parseString(line)
foreach(item tokens
when(item == your_string
printf("Finding %s in line %d\n" your_string lineCnt)
)
)
)
close(inPort)
Or you could use the regular expression functions (e.g. rexCompile, rexExecute,
rexMatchp etc) in IC5141, Or the pcre functions (pcreCompile, pcreExecute,
pcreMatchp etc which add a much more powerful regular expression capability
in IC6.1).

Note, this is for the bit where you're trying to find the line which
contains a particular string. Counting the lines would be similar to the
above.

Regards,

Andrew.
 
You may try this
lineCnt = 0
inPort = infile("fileName")
while(gets(line inPort)!=nil
lineCnt = lineCnt+1
tokens = parseString(line)
foreach(item tokens
when(item == your_string
printf("Finding %s in line %d\n" your_string lineCnt)
)
)
)
close(inPort)

On 2$B7n(B18$BF|(B, $B2<8a(B4$B;~(B44$BJ,(B, bedo...@gmail.com wrote:
Hi,

i'm trying to count the number of line of a text file, using skill.

Actually, this text file is very messy. I'm looking for a particular
string inside ... and i want to copy the full contents of the line
which contents my string.

so my idea is:
1-count the number of line of my textfile
2-reading line after line to find my string (by a loop )

but step 1 in the most difficult for me :s

May someone helps me please ?

thanks,

b.
 

Guest
Hi,

i'm trying to count the number of line of a text file, using skill.

Actually, this text file is very messy. I'm looking for a particular
string inside ... and i want to copy the full contents of the line
which contents my string.

so my idea is:
1-count the number of line of my textfile
2-reading line after line to find my string (by a loop )

but step 1 in the most difficult for me :s


May someone helps me please ?

thanks,

b.
 

Welcome to EDABoard.com

Sponsor

Back
Top