Ameya wrote:
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 */
--
pete