Help on File Reading in C programming.

A

Ameya

i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way


r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.

please help.
Thanks
AD.
 
E

Eric Sosman

Ameya said:
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way


r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.

There are many ways to do this. Here are two:

- Use fgets() to read each complete line into an array
of `char', and use sscanf() to extract the interesting
fields from the array.

- Use scanf() or fscanf() to read the interesting fields
from the beginning of each line, then use a loop like

int ch;
while ((ch = getc(stream) != '\n' && ch != EOF)
;

to skip the rest of the line.
 
P

pete

Ameya said:
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way

r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.

/* BEGIN new.c */

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

#define ARGV_0 "new"
#define MAX_LENGTH 60
#define str(s) # s
#define xstr(s) str(s)

int get_line(FILE *fd, char *line);
int get_line_up_to_left_bracket(FILE *fd, char *line);

int main(int argc, char *argv[])
{
FILE *fd;
char string[MAX_LENGTH + 1];
int rc;

if (argc > 1) {
fd = fopen(argv[1], "r");
if (fd == NULL) {
fprintf(stderr,
"\nfopen() problem with \"%s\"\n", argv[1]);
exit(EXIT_FAILURE);
}
printf("\n%s opened for reading.\n", argv[1]);
rc = get_line(fd, string);
while (rc == 1) {
puts(string);
rc = get_line_up_to_left_bracket(fd, string);
}
fclose(fd);
printf("\n%s closed.\n", argv[1]);
} else {
puts("Usage:\n >" ARGV_0 " <TRACE_FILE.txt> ...\n\n"
"Example:\nC:\\>" ARGV_0 " trace.txt");
}
return 0;
}

int get_line_up_to_left_bracket(FILE *fd, char *line)
{
int rc;

do {
rc = fscanf(fd, "%" xstr(MAX_LENGTH) "[^[\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0);
return rc;
}

int get_line(FILE *fd, char *line)
{
int rc;

do {
rc = fscanf(fd, "%" xstr(MAX_LENGTH) "[^\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0);
return rc;
}

/* END new.c */
 
A

Ameya

Thanks for the help guys !!
Will try it out !

Eric said:
Ameya said:
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way


r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.

There are many ways to do this. Here are two:

- Use fgets() to read each complete line into an array
of `char', and use sscanf() to extract the interesting
fields from the array.

- Use scanf() or fscanf() to read the interesting fields
from the beginning of each line, then use a loop like

int ch;
while ((ch = getc(stream) != '\n' && ch != EOF)
;

to skip the rest of the line.
 
M

Mabden

Ameya said:
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way


r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

The solution is called Perl. It is a language that pulls data from text
files. It has many, many functions to extract text and transform it.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top