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