How to count the number of line in a file ?

Discussion in 'Cadence' started by bedoune, Feb 18, 2008.

  1. bedoune

    bedoune 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.
     
    bedoune, Feb 18, 2008
    #1
  2. bedoune

    Andy Guest

    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)
     
    Andy, Feb 18, 2008
    #2
  3. Andy wrote, on 02/18/08 13:50:
    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.
     
    Andrew Beckett, Feb 18, 2008
    #3
  4. bedoune

    bedoune Guest

    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.
     
    bedoune, Feb 19, 2008
    #4
  5. bedoune

    sc Guest

    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,
     
    sc, Feb 19, 2008
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.