Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: parsing a text line vertically

Reply
Thread Tools

Re: parsing a text line vertically

 
 
André Gillibert
Guest
Posts: n/a
 
      08-20-2007
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 <>
 
Reply With Quote
 
 
 
 
saron78@googlemail.com
Guest
Posts: n/a
 
      08-20-2007
> > 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" <tabkan...@yahoo.fr> wrote:
> On Mon, 20 Aug 2007 23:22:22 +0200, <saro...@googlemail.com> 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 <tabakan...@yahoo.fr>



 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-20-2007
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
 
Reply With Quote
 
saron78@googlemail.com
Guest
Posts: n/a
 
      08-20-2007
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


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-20-2007
"" 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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-20-2007
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
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-21-2007
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a text file line-by-line: skipping badly-formed lines? denis.papathanasiou@gmail.com Perl Misc 27 05-18-2007 07:07 PM
Simple text parsing gets difficult when line continues to next line Jacob Rael Python 7 11-28-2006 10:03 PM
how can align the text vertically in a table cell atin45gupt@gmail.com HTML 1 01-17-2006 04:25 PM
vertically split text? Mark Parnell HTML 11 03-05-2005 10:18 PM
style="POSITION: relative; LEFT: 1in;" doesn't line-up vertically John A Grandy ASP .Net Web Controls 0 12-09-2004 02:39 AM



Advertisments