Re: parsing a text line vertically

Discussion in 'C Programming' started by André Gillibert, Aug 20, 2007.

  1. On Mon, 20 Aug 2007 23:22:22 +0200, <> wrote:

    > Hi,
    > I have the problem of passing of more lines which I find really
    > difficult, cause I am not experienced in C.
    >
    > First Log file.
    > ------------------------- Cat 1------ cat2-----------------
    > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]
    >
    > I can pass it using
    > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s", &c, b, &r, &a, &o,
    > &d, test)
    >
    > but the second log file has the same information stored in the
    > follwing way.
    >
    > testdata
    > cat1 : 91.00 K
    > cat2 : 4.11 K
    >
    > testdata2
    > cat1 : 911.00 K
    > cat2 : 24.11 K
    >
    > I want to abstract cat1 and cat2 for every testdata.
    >
    > But my problem is that I can only read a line at one time.
    >
    > How can I save the position of file and give for every testdata: cat1
    > and cat2 simultaneously.
    >
    > would appreciate any suggestions
    >

    Your question is off-topic.
    comp.std.c is about the C standardization process, the C standard paper:
    defects, ambiguities, improvements of the C standard.

    It's more appropriate on comp.lang.c which discusses programming in
    standard C.

    This message is cross-posted on comp.std.c and comp.lang.c and I set the
    Follow-up header to comp.lang.c, so that further discussion is done on
    comp.lang.c.

    --
    You can contact me at <>
    André Gillibert, Aug 20, 2007
    #1
    1. Advertising

  2. André Gillibert

    Guest

    > > Hi,
    > > I have the problem of passing of more lines which I find really
    > > difficult, cause I am not experienced in C.

    >
    > > First Log file.
    > > ------------------------- Cat 1------ cat2-----------------
    > > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]

    >
    > > I can pass it using
    > > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s", &c, b, &r, &a, &o,
    > > &d, test)

    >
    > > but the second log file has the same information stored in the
    > > follwing way.

    >
    > > testdata
    > > cat1 : 91.00 K
    > > cat2 : 4.11 K

    >
    > > testdata2
    > > cat1 : 911.00 K
    > > cat2 : 24.11 K

    >
    > > I want to abstract cat1 and cat2 for every testdata.

    >
    > > But my problem is that I can only read a line at one time.

    >
    > > How can I save the position of file and give for every testdata: cat1
    > > and cat2 simultaneously.

    >
    > > would appreciate any suggestions









    On Aug 20, 11:49 pm, "André Gillibert" <> wrote:
    > On Mon, 20 Aug 2007 23:22:22 +0200, <> wrote:
    > > Hi,
    > > I have the problem of passing of more lines which I find really
    > > difficult, cause I am not experienced in C.

    >
    > > First Log file.
    > > ------------------------- Cat 1------ cat2-----------------
    > > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]

    >
    > > I can pass it using
    > > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s", &c, b, &r, &a, &o,
    > > &d, test)

    >
    > > but the second log file has the same information stored in the
    > > follwing way.

    >
    > > testdata
    > > cat1 : 91.00 K
    > > cat2 : 4.11 K

    >
    > > testdata2
    > > cat1 : 911.00 K
    > > cat2 : 24.11 K

    >
    > > I want to abstract cat1 and cat2 for every testdata.

    >
    > > But my problem is that I can only read a line at one time.

    >
    > > How can I save the position of file and give for every testdata: cat1
    > > and cat2 simultaneously.

    >
    > > would appreciate any suggestions

    >
    > Your question is off-topic.
    > comp.std.c is about the C standardization process, the C standard paper:
    > defects, ambiguities, improvements of the C standard.
    >
    > It's more appropriate on comp.lang.c which discusses programming in
    > standard C.
    >
    > This message is cross-posted on comp.std.c and comp.lang.c and I set the
    > Follow-up header to comp.lang.c, so that further discussion is done on
    > comp.lang.c.
    >
    > --
    > You can contact me at <>
    , Aug 20, 2007
    #2
    1. Advertising

  3. André Gillibert

    pete Guest

    André Gillibert wrote:
    >
    > On Mon, 20 Aug 2007 23:22:22 +0200, <> wrote:
    >
    > > Hi,
    > > I have the problem of passing of more lines which I find really
    > > difficult, cause I am not experienced in C.
    > >
    > > First Log file.
    > > ------------------------- Cat 1------ cat2-----------------
    > > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]
    > >
    > > I can pass it using
    > > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s",
    > > &c, b, &r, &a, &o, &d, test)
    > >
    > > but the second log file has the same information stored in the
    > > follwing way.
    > >
    > > testdata
    > > cat1 : 91.00 K
    > > cat2 : 4.11 K
    > >
    > > testdata2
    > > cat1 : 911.00 K
    > > cat2 : 24.11 K
    > >
    > > I want to abstract cat1 and cat2 for every testdata.


    I don't know what the result of an abstraction is supposed to be.

    > > But my problem is that I can only read a line at one time.
    > >
    > > How can I save the position
    > > of file and give for every testdata: cat1
    > > and cat2 simultaneously.
    > >
    > > would appreciate any suggestions


    I don't know what "give for every testdata: cat1 and cat2" means.

    --
    pete
    pete, Aug 20, 2007
    #3
  4. André Gillibert

    Guest

    hi ,
    cat1 and cat2 are the same information like in the horizontal version.
    but now i have the data in different rows.

    in first version it is easy to parse line be line.
    but in the second version, I have to go through many lines to get the
    same info.

    my problem is that i can read only one line at a time and I have to do
    all the cat1..cat n till i come to testdatata2 line.



    > > > First Log file.
    > > > ------------------------- Cat 1------ cat2-----------------
    > > > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > > > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]

    >
    > > > I can pass it using
    > > > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s",
    > > > &c, b, &r, &a, &o, &d, test)

    >
    > > > but the second log file has the same information stored in the
    > > > follwing way.

    >
    > > > testdata
    > > > cat1 : 91.00 K
    > > > cat2 : 4.11 K

    >
    > > > testdata2
    > > > cat1 : 911.00 K
    > > > cat2 : 24.11 K

    >
    > > > I want to abstract cat1 and cat2 for every testdata.

    >
    > I don't know what the result of an abstraction is supposed to be.
    >
    > > > But my problem is that I can only read a line at one time.

    >
    > > > How can I save the position
    > > > of file and give for every testdata: cat1
    > > > and cat2 simultaneously.

    >
    > > > would appreciate any suggestions

    >
    > I don't know what "give for every testdata: cat1 and cat2" means.
    >
    > --
    > pete
    , Aug 20, 2007
    #4
  5. André Gillibert

    CBFalconer Guest

    "" wrote:
    >
    > cat1 and cat2 are the same information like in the horizontal
    > version. but now i have the data in different rows.
    >
    > in first version it is easy to parse line be line. but in the
    > second version, I have to go through many lines to get the same
    > info.
    >
    > my problem is that i can read only one line at a time and I have
    > to do all the cat1..cat n till i come to testdatata2 line.


    Please do not top-post. Your answer belongs after (or intermixed
    with) the quoted material to which you reply, after snipping all
    irrelevant material. See the following links:

    --
    <http://www.catb.org/~esr/faqs/smart-questions.html>
    <http://www.caliburn.nl/topposting.html>
    <http://www.netmeister.org/news/learn2quote.html>
    <http://cfaj.freeshell.org/google/> (taming google)
    <http://members.fortunecity.com/nnqweb/> (newusers)


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Aug 20, 2007
    #5
  6. André Gillibert

    pete Guest

    wrote:
    >
    > hi ,
    > cat1 and cat2 are the same information like in the horizontal version.
    > but now i have the data in different rows.
    >
    > in first version it is easy to parse line be line.
    > but in the second version, I have to go through many lines to get the
    > same info.
    >
    > my problem is that i can read only one line at a time and I have to do
    > all the cat1..cat n till i come to testdatata2 line.


    I still don't understand what your output result is supposed to be.
    But,
    I frequently like to read an entire text file into a linked list,
    converting the lines to strings, one string per node,
    and then proccess the data in the list.

    type_1.c shows one way to read an entire text file into a linked list.

    /* BEGIN type_1.c */

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

    #define ARGV_0 "type_1"

    struct list_node {
    struct list_node *next;
    void *data;
    };

    int get_line(char **lineptr, size_t *n, FILE *stream);
    void list_free(struct list_node *node, void (*free_data)(void *));
    int list_fputs(struct list_node *node, FILE *stream);
    struct list_node *string_node(struct list_node **head,
    struct list_node *tail,
    char *data);

    int main(int argc, char *argv[])
    {
    int rc;
    FILE *fp;
    char *buff_ptr;
    size_t buff_size;
    struct list_node *head, *tail;

    if (argc > 1) {
    buff_size = 0;
    buff_ptr = NULL;
    tail = head = NULL;
    while (*++argv != NULL) {
    fp = fopen(*argv, "r");
    if (fp != NULL) {
    while ((rc = get_line(&buff_ptr, &buff_size, fp))
    > 0)

    {
    tail = string_node(&head, tail, buff_ptr);
    if (tail == NULL) {
    break;
    }
    }
    fclose(fp);
    switch (rc) {
    case EOF:
    if (buff_ptr != NULL
    && strlen(buff_ptr) != 0)
    {
    puts("rc equals EOF\n"
    "The string in buff_ptr is:");
    puts(buff_ptr);
    tail = string_node(&head, tail, buff_ptr);
    }
    break;
    case 0:
    puts("realloc returned a null pointer "
    "value in line_to_string.");
    if (buff_size > 1) {
    puts("rc equals 0\n"
    "The string in buff_ptr is:");
    puts(buff_ptr);
    tail = string_node(&head, tail, buff_ptr);
    }
    break;
    default:
    puts("malloc problem in string_node.");
    break;
    }
    } else {
    printf("\nfopen() problem with \"%s\"\n", *argv);
    break;
    }
    list_fputs(head, stdout);
    list_free(head, free);
    head = NULL;
    }
    free(buff_ptr);
    } else {
    puts("Usage:\n>" ARGV_0
    " <TEXT_FILE_0> <TEXT_FILE_1> <TEXT_FILE_2> ...\n");
    }
    return 0;
    }

    int get_line(char **lineptr, size_t *n, FILE *stream)
    {
    int rc;
    void *p;
    size_t count;

    count = 0;
    while ((rc = getc(stream)) != EOF) {
    if (count != -1) {
    ++count;
    }
    if (count + 2 > *n) {
    p = realloc(*lineptr, count + 2);
    if (p == NULL) {
    if (*n > count) {
    (*lineptr)[count] = '\0';
    (*lineptr)[count - 1] = (char)rc;
    } else {
    if (*n != 0) {
    **lineptr = '\0';
    }
    ungetc(rc, stream);
    }
    count = 0;
    break;
    }
    *lineptr = p;
    *n = count + 2;
    }
    if (rc == '\n') {
    (*lineptr)[count - 1] = '\0';
    break;
    }
    (*lineptr)[count - 1] = (char)rc;
    }
    if (rc != EOF) {
    rc = count > INT_MAX ? INT_MAX : count;
    } else {
    if (*n > count) {
    (*lineptr)[count] = '\0';
    }
    }
    return rc;
    }

    void list_free(struct list_node *node, void (*free_data)(void *))
    {
    struct list_node *next_node;

    while (node != NULL) {
    next_node = node -> next;
    free_data(node -> data);
    free(node);
    node = next_node;
    }
    }

    int list_fputs(struct list_node *node, FILE *stream)
    {
    while (node != NULL) {
    if (fputs(node -> data, stream) == EOF) {
    return EOF;
    }
    if (putc('\n', stream) == EOF) {
    return EOF;
    }
    node = node -> next;
    }
    return '\n';
    }

    struct list_node *string_node(struct list_node **head,
    struct list_node *tail,
    char *data)
    {
    struct list_node *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
    node -> next = NULL;
    node -> data = malloc(strlen(data) + 1);
    if (node -> data != NULL) {
    strcpy(node -> data, data);
    if (*head == NULL) {
    *head = node;
    } else {
    tail -> next = node;
    }
    } else {
    free(node);
    node = NULL;
    }
    }
    return node;
    }

    /* END type_1.c */


    > > > > First Log file.
    > > > > ------------------------- Cat 1------ cat2-----------------
    > > > > 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    > > > > 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]

    > >
    > > > > I can pass it using
    > > > > sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s",
    > > > > &c, b, &r, &a, &o, &d, test)

    > >
    > > > > but the second log file has the same information stored in the
    > > > > follwing way.

    > >
    > > > > testdata
    > > > > cat1 : 91.00 K
    > > > > cat2 : 4.11 K

    > >
    > > > > testdata2
    > > > > cat1 : 911.00 K
    > > > > cat2 : 24.11 K

    > >
    > > > > I want to abstract cat1 and cat2 for every testdata.

    > >
    > > I don't know what the result of an abstraction is supposed to be.
    > >
    > > > > But my problem is that I can only read a line at one time.

    > >
    > > > > How can I save the position
    > > > > of file and give for every testdata: cat1
    > > > > and cat2 simultaneously.

    > >
    > > > > would appreciate any suggestions

    > >
    > > I don't know what "give for every testdata: cat1 and cat2" means.


    --
    pete
    pete, Aug 21, 2007
    #6
  7. André Gillibert

    Mark Bluemel Guest

    André Gillibert wrote:
    > On Mon, 20 Aug 2007 23:22:22 +0200, <> wrote:
    >
    >> Hi,
    >> I have the problem of passing of more lines which I find really
    >> difficult, cause I am not experienced in C.
    >>
    >> First Log file.
    >> ------------------------- Cat 1------ cat2-----------------
    >> 19.867: [Testdata 98095K->24545K(942080K), 0.1468955 secs]
    >> 19.845: [Testdata2 98095K->24545K(942080K), 0.1468955 secs]
    >>
    >> I can pass it using
    >> sscanf(line, "%*s %s %s %dK->%dK(%d%*s %d.%s %*s", &c, b, &r, &a, &o,
    >> &d, test)
    >>
    >> but the second log file has the same information stored in the
    >> follwing way.
    >>
    >> testdata
    >> cat1 : 91.00 K
    >> cat2 : 4.11 K
    >>
    >> testdata2
    >> cat1 : 911.00 K
    >> cat2 : 24.11 K
    >>
    >> I want to abstract cat1 and cat2 for every testdata.


    I think you mean "extract"

    >>
    >> But my problem is that I can only read a line at one time.


    And that's only a problem because you make it one...

    >>
    >> How can I save the position of file and give for every testdata: cat1
    >> and cat2 simultaneously.


    If the format is totally consistent, you can simply have a block of code
    which reads 4 lines at a time, surely...

    For a more general solution, you could use a state machine (google for
    the term, perhaps). States in this case are probably
    LOOKING_FOR_TESTDATA, EXPECTING_CAT1, EXPECTING_CAT2,
    PROCESSING_TESTDATA, in that order...
    Mark Bluemel, Aug 21, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Parnell

    vertically split text?

    Mark Parnell, Mar 3, 2005, in forum: HTML
    Replies:
    11
    Views:
    910
    Day Brown
    Mar 5, 2005
  2. Replies:
    1
    Views:
    490
  3. Trevor

    CSS Vertically Align Text

    Trevor, Apr 28, 2006, in forum: HTML
    Replies:
    5
    Views:
    9,446
    Barbara de Zoete
    Apr 29, 2006
  4. bruce barker
    Replies:
    1
    Views:
    884
    =?Utf-8?B?UkFNRVNIIEJBQlUgVkVFUkFNQU5JIChSQU1CQUIp
    Jan 11, 2007
  5. John A Grandy

    style="POSITION: relative; LEFT: 1in;" doesn't line-up vertically

    John A Grandy, Dec 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    120
    John A Grandy
    Dec 9, 2004
Loading...

Share This Page